5 ms·
Implicit variable declaration “doesn’t work”, he says. Those millions upon millions of developers creating real world value using Python sure are going to be su
by bandyaboot 3y ago
Implicit variable declaration “doesn’t work”, he says. Those millions upon millions of developers creating real world value using Python sure are going to be surprised when they find out.
- kybernetikos 3y agoWell there's a reason that JavaScript used to support this but no longer does. Characterising it as 'implicit variable declaration doesn't work' seems like a fairly reasonable summary of the view of a very large number of people. To be true it doesn't need to be impossible to write code in a language with implicit declaration, it just needs to be significantly worse.
- bazoom42 3y agoIn JavaScript, implicit varible declarations goes to the global scope, which is obviously a footgun. In python they go to the local scope as you would expect.
- kybernetikos 3y agoGoing to local scope is obviously better, but it still allows misspellings (some of which are very difficult to see) to create new variables and have code that looks like it works but does something entirely different to what you expect.
- gary_0 3y agoTo be fair, most of his talk is biased towards compiled and statically typed languages (topics like compiler switches, ABI, etc). Not that compiled languages can't have implicit variables, but a lot of people would find it surprising behavior. People familiar with compiled languages expect the compiler to be strict and tend to dislike it when it guesses (especially when it guesses wrong).
- kazinator 3y agoPython doesn't have implicit declaration. The first occurrence of the assignment syntax declares the variable and binds it to a value. Proof that Python doesn't have implicit declaration: >>> foo Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'foo' is not defined Pyhon's design sin is that it fails to distinguish the binding of a variable from assignment. The programmer cannot locally (i.e. just in that statement alone) express "I expect this statement to be freshly creating a variable, introducing it into the scope, with value 42" and "I expect to be assigning value 42 to a variable which already exists in the scope (or else this should be diagnosed)". An example of a language actually without declaration is Awk. This helps one-liners be very terse, but hinders large programs. Proof that awk has implicit declaration in contrast to Python: $ awk 'BEGIN { print foo }' $ No errors. The shell has implicit declaration: $ echo $foo You turn it off in serious scripts with "set -u" to have it diagnosed.
- adrianmsmith 3y ago> Pyhon's design sin is that it fails to distinguish the binding of a variable from assignment. The programmer cannot locally (i.e. just in that statement alone) express "I expect this statement to be freshly creating a variable, introducing it into the scope, with value 42" and "I expect to be assigning value 42 to a variable which already exists in the scope (or else this should be diagnosed)". I'm not entirely sure this is a bad thing? I'm learning Go right now, and I'm encountering plenty of code like: err := DoFoo() if err != nil .... err = DoBar() if err != nil .... The problem with that is that if you remove the DoFoo line you have to change the DoBar line, even though nothing's really changed there. An (albeit small) waste of time, an extra thing to think about which has nothing to do with the problem you're solving, more lines to review in the PR, and more chance of merge conflicts later.
- hamdouni 3y agoYou can avoid this by using : if err := DoFoo(); err != nil { ... The 'err' variable will be local to the 'if' so you can write also : if err := DoBar(); err != nil { ... It is also nice to write it in one line :-)
- pjmlp 3y agoThey actually do, when they have to track down a bug caused by a typo, or have the happy task to refactor a piece of code and forget a couple of places.
- IshKebab 3y agoCome on, obviously when he's saying it "doesn't work" he doesn't mean "it makes it impossible to use the language to write software".