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
  comtypes: how to provinde a list of string to a COM method zalanthas 0 368 Jun-26-2024, 01:27 PM
Last Post: zalanthas
  how to capitalize letter in list object Python iwonkawa 4 642 May-29-2024, 04:29 PM
Last Post: DeaD_EyE
  How do you get Python to print just one value in a list? 357mag 3 1,421 May-17-2023, 09:52 PM
Last Post: rob101
  add object and name in list 3lnyn0 4 1,723 Nov-24-2022, 07:33 PM
Last Post: buran
  SystemError: <method-wrapper '__getattribute__' of EnumMeta object sciloop 4 2,050 Jul-23-2022, 07:57 PM
Last Post: sciloop
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 6,093 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  Print List to Terminal DaveG 2 1,762 Apr-02-2022, 11:25 AM
Last Post: perfringo
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 2,501 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 17,002 Jan-19-2022, 08:33 AM
Last Post: menator01
  Question about List.reverse() method tomliuwhite 1 1,710 Dec-07-2021, 08:20 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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