Python Forum

Full Version: print all method and property of list object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'
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
engmoh Wrote:can i replace "i" to value of "i" immediately without getattr() function
No. 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:04 PM)Gribouillis Wrote: [ -> ]Python's designers prefer function calls over exotic syntactic constructs.
thank you for your advice and deceleration