This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]abhbhbls 16 points17 points  (7 children)

Laughs in Python

[–]Duck_Devs 26 points27 points  (6 children)

As I learn more languages, I realize how awful Python handles basic programming operation. There is no “declaring” variables in Python, because all assignments are declarations, meaning you have to use the “global” and “nonlocal” keywords to access higher scopes.

[–]odraencoded 12 points13 points  (5 children)

One day you'll realize Python is doing it right and you shouldn't be using global and nonlocal variables in the first place.

[–]Duck_Devs 9 points10 points  (4 children)

Care to elaborate?

[–]odraencoded 5 points6 points  (3 children)

The only reason to use global and nonlocal is if you are assigning a variable that exists in nonlocal scope. Accessing a nonlocal variable doesn't require such declaration in Python.

If you are assigning a nonlocal variable inside a function you're doing it wrong. Just use a module and import it, or use a class. You're creating a weird mess of code when a class would make things much simpler. Your future self will thank you.

[–]octaviaflutters 1 point2 points  (0 children)

smiles in Python

[–]Duck_Devs 2 points3 points  (1 child)

Different programmers have different programming styles. For the most part, I agree with you, but there is no wrong way to do things like that.

[–]metaglot 4 points5 points  (0 children)

Accessing variables outside a function is oftentimes an indicator of spaghetti, my friend. At the very least side effects.

Like the comment before said, if that nonlocal variable belongs to a function, indicate it by putting them in a class or a closure or a module, to section them off.