Example code from a module:
somevar = "a"
def myfunc(somevar = None):
# need to access both somevars ???
# ... if somevar was specified print it or use the global value
pass
if __name__ == '__main__':
somevar = "b" # this is just for fun here
myfunc("c")
myfunc() # should print "a" (the value of global variable)
There are at least two reasons for using the same name:educational (to learn how to use local/globals) and usage in modules.
Let say that this code is part of your module: mymodule
and you want to do things like:
import mymodule
mymodule.samevar = "default"
...
mymodule.myfunc(somevar = "a")
...
mymodule.myfunc()
As you can imagine in this example is simplified, imagine that somevar
parameter is one of many optional parameters and that myfunc is called in many places.