8
Wisecrack
26d

I wasn't gone, I was just working.

Anyway, I had some fun and wrote a simple 10 minute little precursor to an ngram implementation:

(when your comments are as long as the code, lol)
https://pastebin.com/bZVh8YSP

It obviously doesn't do type checking, or valid value checking, or any of that, and there may be an old comment or two adding cruft but whatever

Comments
  • 4
    Wow you write shitty python.

    1. docstrings, not comments
    2. type annotations, not types in docstrings
    3. use "if x is None", not equality (None is special)
    4. use doctest blocks, not "example usage"
    5. you're slicing to extract result twice, just append result
    6. probably other things...
  • 8
    @atheist pretty normal code for a mathematician
  • 6
    @Lensflare fuck you, I'm a mathematician (unfortunately, however, very fucking true)
  • 6
    I want to kill some people. I have liners and type checkers turned up all the way for this very reason. I'm also writing a docstring linter so it can bitch at them so I don't have to.
  • 4
    @atheist thank you.

    Comments over docstrings have always just been a preference of mine.

    Why prefer 'if x is None' over equality testing?

    "you're slicing twice."

    Fuck, you're right. Can't believe I didn't catch that. Thanks.
  • 1
    @atheist very commendable
  • 3
    @Wisecrack different languages, different strokes. In python it's defined by the language that a string literal at the start of the function is the documentation string for that function. It can be accessed at runtime (the help function reads it), and tools interpret it as such. It can also be accessed as "x.__doc__"
  • 2
    X is None is because None is effectively a singleton, the identity check is faster than the equality check. It's probably also in a PEP, and linters will all complain about equality with None
  • 1
    I also don't understand your overflow comment in the code. Python ints are arbitrary size, lists you'll probably run out of memory before you hit the size limit
  • 1
    @atheist I believe you mean linTers?
  • 2
    @CoreFusionX you didn't see anything...
  • 1
    @atheist I was shocked by the python string literal thing and looked it up. You should have mentioned that it‘s a comment (prefixed with #), not an actual string literal 😂
  • 0
  • 0
    @Lensflare confused. OP is using a comment, the language uses a string literal, ie

    def foo():
    """This is a docstring"""
  • 2
    The docstring gets attached to the function object by the language.
  • 2
    OMG the sexual tension between the `w` and `stride` variables...
  • 0
    @atheist linters are workless since black. Does black also sort imports? Not sure
  • 1
    @retoor isort will sort imports. I don't like black, in places I think it makes code less readable and it's too stubborn to include things like "the ability to disable some rules"
  • 1
    I write complex mathematical code, readability trumps the petty opinions of some stupid lint devs 100% of the time
  • 1
    @atheist when I looked it up, it was

    def foo():
    # This is a docstring
  • 0
    @Lensflare that might have been a thing 10 years ago... 😅
  • 0
  • 1
    @atheist My god… so it IS as retarded as you said originally. This is mind boggling!
  • 0
    @Lensflare how so? It's like a C multiline comment. But introspectable
  • 1
    @atheist you don‘t see any problem with it?
  • 1
    @Lensflare the # character is noise, the thing that is pertinent is the text. Some docstrings are dozens of lines. Reduce noise, improve readability. I see no problem with it. It also moves docstrings to a first class entity, a language feature instead of a feature provided by your tooling. C++, docstrings are generated from specially formatted comments. Same with Java. Documentation isn't a comment. I think it's an improvement over them. Genuinely intrigued why you don't like it.
  • 0
    @atheist imo, comments and doc are not code, so they should be… comments.

    What if my function is supposed to simply return a string literal?
    Would this string also count as a documentation for the function?
    This seems insane. The doc and the returned value are completely different things.

    What’s the problem with comments? If it’s about multiline, then there should be syntax for multiline comments.
    Abusing string literals for that just because they support multiline is insane.
    And even if there is no multiline support for comments, I’d still prefer multiple lines prefixed with #.

    I also don’t get the point about retrospection. Why is it relevant for the runtime to be able to extract the docs for a function?
  • 1
    @atheist oh, thats cool and useful. Can't believe I didn't fucking know this before now.

    Thank you, thank you, thank you.
  • 1
    @Lensflare it only applies to string literals that aren't used (assigned to a variable, passed to a function or returned). So returning a string literal doesn't make that literal the docstring of the function.

    There is syntax for multiline comments. It's an unused multiline string. You're not a very good programmer are you?
  • 0
    @atheist yes, I get it. It’s an unused string literal. I also get that there is no ambiguity.
    That doesn’t mean that I agree that it’s a good thing.

    It’s still arbitrary to use the first unused string expression or literal as the doc.

    And you haven’t given me an explanation yet for why this is better than a normal comment and why it’s good for the runtime to be able to have access to it.
    It’s not like there is a use case where I run the code and somehow want to get the docs for the functions that are executed. I want the docs when I write the code, not when I run it.

    > You are not a very good programmer, are you?
    Where does this suddenly come from?
  • 0
    @Lensflare do you know what a repl is? Python has this function called "help", guess what it does and how it does it?
  • 0
    You seem like you're being intentionally dense.
  • 0
    @Lensflare it's not "the first", it's the one immediately after the function declaration. If there's any other expression after the function declaration then it's a multiline comment.
  • 1
    Having it runtime accessible and part of the language makes building tooling easier. You don't need to parse code to get the docstring. You just import an object and pass it to a function in the inspect module. Or parse the code into an AST and extract the docstring attribute. AST parsing is available at runtime. Built into the language.

    Which is easier than having to write a customised parser to extract comments that are formatted in your special way.
  • 1
    "it's arbitrary" Welcome to human fucking existence. Most conventions are. You're trying to tell me a triple forward slash isn't arbitrary?
  • 0
    Like, seriously, python, the language that made notebooks popular (ipython/jupyter), repls popular (the python repl terminal), "I don't want docs when I run the code, only when I write it". You're wrong and blind.
  • 1
    @atheist you can stop your insults, your point about the repl has convinced me. That’s the kind of argument I was looking for.
  • 2
    @Lensflare fair, I'm sorry.
  • 1
    @atheist it's kinda beautiful actually.
Add Comment