Live data from Hacker News

Python: Even a Feature That You Do Not Use Can Bite You

blog.petrzemek.net

11–20 of 48 posts

Re: Python: Even a Feature That You Do Not Use Can Bite You

#11
post #3

I mean technically it's a feature you're using, just unintentionally. Kinda like setting a mutable object as a default argument. But I'll admit I got bit by exactly this recently, wrote up an "assignment" using a colon instead of an equal sign and it didn't do anything, but didn't fail either. Somewhat frustratingly, pycharm didn't warn about it either. I should probably check their bug tracker to see if somebody alr…

I don't think so, because 42 is not a type in Python (it could be one day, though - in TypeScript, 42 can be used as a type, basically an enum with only one value). I think (naively) that it should throw an error.

Re: Python: Even a Feature That You Do Not Use Can Bite You

#12
This is a weird article to me. There seems to be an implicit condemnation of Python for this new feature resulting in non-error, unexpected behavior under certain typoes, despite the fact that that is true for literally any typo that results in valid code in any language (such as, e.g., = instead of ==). I don't understand how it would be possible to add a new syntactic option without being "bit".

Regarding type hints' ability to be arbitrary code, I suspect this is to allow you to use nested type hints from the `typing` library, like `Tuple[int, int]`, as well as more complex or user-defined types. Plus it just gives more flexibility in general.

I'd be interested to hear the author's suggestions on alternative syntax or ways to catch when the programmer intended something other than a type hint, or even arguments that type hints are extraneous and should not have been added - personally, I've found them quite useful recently, in combination with pylint.

Re: Python: Even a Feature That You Do Not Use Can Bite You

#14
In any language that undergoes syntactic extension, there is new syntax that is a syntax error in an old version of the language. Erroneous programs written for the old version which land on that previously bad syntax will undergo a change in behavior.

This is true even in languages that don't undergo syntactic extension. If you call a nonexistent function today, your program bombs. Tomorrow, that function could be added, so now something unexpected happens, perhaps.

Lisps are susceptible. Today (foo bar) is diagnosed; tomorrow it's new syntax (not simply a new function), because foo exists as a macro operator.

However, functions and macros, being named, can be namespaced to curtail such issues. Random, ad hoc read syntax extensions, not so much.

One approach is that files, or sections of files, can support annotations which indicate what version of the syntax is required.

Under GCC, if you want only C89, so that any syntactic extensions from later dialects are invalid syntax, you use '-ansi' on the command line to select that dialect. For oinstance, support for // comments disappears, and so x //blah/ y is x / y, rather than x followed by a comment-to-end-of-line.

Re: Python: Even a Feature That You Do Not Use Can Bite You

#16
post #5

This is true. I don't think Python is to blame for it, though. Any language has potential for making a mistake that's syntactically valid but doesn't have the intended meaning. It's like when a former manager of mine typed "call me at your continence" when he meant to say "call me at your convenience".

Languages can help manage change, though, providing features such that programmers working against older dialects of the language, who make mistakes, get diagnostics according to the old version, rather than accidentally stepping on new syntax.

Re: Python: Even a Feature That You Do Not Use Can Bite You

#18
This isn't a good article to post. It's just one person complaining about a change he didn't know about.

It's really just a syntax change. I probably wouldn't have chosen that particular syntax using colons, or I would have forced another keyword like "set" in front of it, but it is what it is. He created a bug and he likely won't do it again, so problem solved.

Re: Python: Even a Feature That You Do Not Use Can Bite You

#19
post #15

This is an idiotic article because a static analyzer like mypy will find this in a hurry: > error: Invalid type: try using Literal[42] instead? If you don't use the analyzer, the problem is you.

The question, then, is why that kind of analysis isn't built into the compiler?

Re: Python: Even a Feature That You Do Not Use Can Bite You

#20
post #3

I mean technically it's a feature you're using, just unintentionally. Kinda like setting a mutable object as a default argument. But I'll admit I got bit by exactly this recently, wrote up an "assignment" using a colon instead of an equal sign and it didn't do anything, but didn't fail either. Somewhat frustratingly, pycharm didn't warn about it either. I should probably check their bug tracker to see if somebody alr…

I don't think so, because 42 is not a type in Python (it could be one day, though - in TypeScript, 42 can be used as a type, basically an enum with only one value). I think (naively) that it should throw an error.

I'm not sure how feasible throwing an error is - Type hints in Python can be arbitrary classes or instances thereof, in order to type hint for stuff like nested data structures (`Dict[str: int]`). You could disallow instances of specific built-in classes, but that seems... clunky, especially as some can be used in valid code (strings are used as forward references).
Post reply on HN