Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function attributes

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?

like image 742
Andrew Youdin Avatar asked May 25 '26 07:05

Andrew Youdin


1 Answers

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.

like image 103
tMC Avatar answered May 27 '26 21:05

tMC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!