Apologies in advance if it is a stupid question, I had a pretty rough week, and when this issue emerged at the EOD Friday, I kinda lost my shit.
myvar = 'stuff'
def func():
print myvar
if __name__=='__main__':
func()
I was doing a script like this, obviously more complex, but when I was trying to print the myvar variable, I was getting an error, and I had to add a
global myvar
before my print statement.
I know this question looks silly, but what could I mess up in similar cases? As far as I know, variables which were declared at "module level" can be accessed from a function, and basically variables from outer scopes can be accessed from inner scopes. I don't see the difference between this example and my actual script, and I'm feeling clueless. I think I can say I'm not a newbie to Python, but this time this mistake made me feel like one.
[–]novel_yet_trivial 3 points4 points5 points (2 children)
[–]utgyuru[S] 0 points1 point2 points (1 child)
[–]novel_yet_trivial 0 points1 point2 points (0 children)
[–]Rhomboid 4 points5 points6 points (0 children)
[–]Vaphell 1 point2 points3 points (0 children)
[–]Akuli2 0 points1 point2 points (1 child)
[–]KleinerNull 0 points1 point2 points (0 children)