Python Forum
print all method and property of list object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print all method and property of list object
#1
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'
Reply
#2
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.
Reply
#3
thank you very much

can i replace "i" to value of "i" immediately without getattr() function
Reply
#4
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.
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do you get Python to print just one value in a list? 357mag 3 1,083 May-17-2023, 09:52 PM
Last Post: rob101
  add object and name in list 3lnyn0 4 1,338 Nov-24-2022, 07:33 PM
Last Post: buran
  SystemError: <method-wrapper '__getattribute__' of EnumMeta object sciloop 4 1,590 Jul-23-2022, 07:57 PM
Last Post: sciloop
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 5,049 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  Print List to Terminal DaveG 2 1,492 Apr-02-2022, 11:25 AM
Last Post: perfringo
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,910 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,252 Jan-19-2022, 08:33 AM
Last Post: menator01
  Question about List.reverse() method tomliuwhite 1 1,422 Dec-07-2021, 08:20 AM
Last Post: ndc85430
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,904 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,412 Jun-15-2021, 03:48 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020