I’ve been reading about getattr() function in dive into python. From that book I know it works great with modules – no questions about that! I’ve conducted some test and I don’t know what’s wrong with that snippet of code where I use getattr() with a specific variable:
var="lol"
a=getattr(var,"join")
a(["A","B","C"]) # output: 'AlolBlolC'
var="lll"
a(["A","B","C"]) # output: 'AlolBlolC' - I thought It should have been 'AlllBlllC'
On the other hand:
var.join(["A","B","C"]) # output:'AlllBlllC'
I don’t understand why after changing variable ‘var’ on ‘111’ it doesn’t work with reference ‘a’ but works in normal way with variable name.
In Python, assignment to a variable doesn’t change the object that variable references. It merely assigns a different object to the variable. The method held by
aremains bound to the stringvarreferenced when you calledgetattr().