4 ms·
Python is a dynamic language, that's what dynamic languages do, you don't have a type checker but have greater flexibility, but you don't have to settle on that
by takeda 7y ago
Python is a dynamic language, that's what dynamic languages do, you don't have a type checker but have greater flexibility, but you don't have to settle on that, you can actually use mypy and annotate types and get best out of both worlds.
> And another one, I typoed a variable name as groups_keys instead of group_keys (or vice-versa, I don't remember). Instead of just throwing an error, Python happily went along with it, used an uninitialized value, and then all the logic broke.
This isn't what Python would do, if the variable was undefined Python will throw an error, so you must have defined it with this name or you're misremembering what have happened.
- ben509 7y agoIt has nothing to do with static vs. dynamic. There's no reason that being an early-binding language that a string has to be iterable itself, and the proposal to change this was only rejected as it broke too many things[1] and couldn't be automatically fixed. Point in the GP's favor: Fixing it would definitely not be a problem with an early-binding language! In fact, the nigh-impossibility of automated refactoring puts lie to the notion that late-binding languages are more "agile." It's a design flaw, in the same way Python 2's allowing comparisons between different types was a flaw, e.g. "a" < 3 succeeds. Python 3 now, correctly, throws a TypeError because there's no sensible ordering between the two things. (While I'm griping: Another design flaw is conflating iterables and iterators, which makes generators almost useless. Say a generator is pass to a function expecting an iterable. If the function uses it twice, the second time it silently returns nothing!) > This isn't what Python would do, if the variable was undefined Python will throw an error I think GP must have assigned to the name, in which case Python will create a lexically bound name. Python's rules for naming can make perfect sense or be quite surprising: try: x = four() except Thing: x = 5 print(x) # 4 or 5 for a in [1, 2, 3]: pass print(a) # 3 ?! [1]: https://mail.python.org/pipermail/python-3000/2006-April/000759.html https://mail.python.org/pipermail/python-3000/2006-April/000...
- nabdab 7y agoWhat’s supposed to be the surprising thing? Are you confusing pass and break and expecting it to print 1?
- ben509 7y agoWhat's surprising, for people used to block semantics, is that `a` survives outside the for loop at all.
- karmakaze 7y agoIt's only surprising if you expect it to behave like another language. Python variables are function (not block) scoped.
- mochomocha 7y agomypy is a great effort, but very experimental. Try using it on any real-world large enough project and it loses most of its value as there are still a lot of unimplemented things, or because you'll depend on a third party module that hasn't support for it yet.
- Jeff_Brown 7y agoCase in point: Pandas, the foundation of data programming in Python, does not provide the Series or DataFrame (that's a table) types in a way that MyPy can use.
- PaulMest 7y agoI'm pretty sure this is what they meant... def my_func(): group_keys = 'My thing' while group_keys == 'My Thing': if some_logic() == 42: groups_key = 'My other thing' # typo here