Oct-26-2019, 01:31 PM
lst=[1,2,5] d=dir(lst)#content all methon and properties for i in d: print(lst.i)if i run i have
AttributeError: 'list' object has no attribute 'i'
print all method and property of list object
|
Oct-26-2019, 01:31 PM
lst=[1,2,5] d=dir(lst)#content all methon and properties for i in d: print(lst.i)if i run i have AttributeError: 'list' object has no attribute 'i'
Oct-26-2019, 02:02 PM
Use
print(getattr(list, i)) instead of print(list.i) because the name of the attribute is not 'i' but the value of the variable i.
thank you very much
can i replace "i" to value of "i" immediately without getattr() function
Oct-26-2019, 05:04 PM
(This post was last modified: Oct-26-2019, 05:04 PM by Gribouillis.)
engmoh Wrote:can i replace "i" to value of "i" immediately without getattr() functionNo. There is no special syntax to access an attribute which name is a variable's value. You may regret this but it's one of the many things that make the great consistency of the language and therefore its success. Python's designers prefer function calls over exotic syntactic constructs.
Oct-26-2019, 05:33 PM
|
|