Today I found some weird behavior of following piece of code:
if (arg == 0):
# some local variable
format = ""
ret = format + arg
else:
# bultin format function
ret = format(arg, "#x")
print ret
It acts different inside and outside function. With this code:
import sys
def foo(arg):
if (arg == 0):
# some local variable
format = ""
ret = format + "0"
else:
# bultin format function
ret = format(arg, "#x")
print ret
arg = int(sys.argv[1])
print "Outside function:"
if (arg == 0):
# some local variable
format = ""
ret = format + "0"
else:
# bultin format function
ret = format(arg, "#x")
print ret
print "Foo call:"
foo(arg)
I get following output of call: python format.py 1
Outside function:
0x1
Foo call:
Traceback (most recent call last):
File "format.py", line 31, in <module>
foo(arg)
File "format.py", line 10, in foo
ret = format(arg, "#x")
The first question is why a local variable under if statement hides the format function used in else statement?
The second one is why it acts different (and now with the expected behavior) when called outside a function?