I want to use function attributes to set variable values as an alternate to using global variables. But sometimes I assign another short name to a function. The behavior seems to always do what I want, i.e. the value gets assigned to the function whether I use the long or short name, as shown below. Is there any danger in this?
def flongname():
pass
f = flongname
f.f1 = 10
flongname.f2 = 20
print flongname.f1, f.f2
And the last line returns 10 20 showing that the different function names refer to the same function object. Right?
id shows that the both f and flongname are references to the same object.
>>> def flongname():
... pass
...
>>> f = flongname
>>> id(f)
140419547609160
>>> id(flongname)
140419547609160
>>>
so yes- the behavior you're experiencing is expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With