DEV Community

Discussion on: From Javascript to Python

Collapse
 
kurisutofu profile image
kurisutofu • Edited

One thing that could be added is that when updating a global variable from a function, the variable needs to be declared as global in the function.

outerVar = "not assigned"

def assign():
   global outerVar
   outervar = "Assigned"
Collapse
 
jannikwempe profile image
Jannik Wempe

Good hint, but in my opinion it doesn't belong to this kind of introduction to python, because it is (in most cases) bad practise. The "normal" case should be to use parameters and return a value.

Collapse
 
kurisutofu profile image
kurisutofu

Well, he introduced variables and functions, so I think it's worth mentioning because should they want to use it, they may think they are used the same way as many other languages and wonder why the variable is not updated.

Since this is geared to people coming from Javascript, I'm assuming they already know when or when not use a global variable.

Thread Thread
 
jannikwempe profile image
Jannik Wempe

Okay, good points, thanks. The argument, that people should already know what they are doing, convinced me. So maybe, yes, it is worth to mention it.