3 ms·
The main 'problem' there is the `global` keyword :^) (it's not needed for read access)
by diarrhea 5y ago
The main 'problem' there is the `global` keyword :^) (it's not needed for read access)
- amelius 5y agoHow would you read foo if it is hidden by a local parameter.
- diarrhea 5y agoI guess not at all. It's shadowed. But this being Python, this is a way: foo = 123 def lol(foo="bar"): print(globals()["foo"], foo) That will output "123 bar". But it's terrible! Your example is a SyntaxError, it doesn't even compile: In [1]: def myfunc(foo="bar"): ...: global foo Input In [1] global foo ^ SyntaxError: name 'foo' is parameter and global
- amelius 5y ago> But this being Python, this is a way That's why I said nonlocal or global ;)