且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在给定特定索引的对象列表中访问和对象属性?

更新时间:2023-09-18 14:05:16

在Python中,访问对象属性语法是

 < object instance>。< attribute_name> 

在您的情况下,[index]。



dir(< object_instance> [index])来获取所有属性,方法关联的对象实例。

您还可以使用for循环迭代

  for&lt ; OBJ&GT;在< object_list>中:
< obj>。< attribute_name>

希望有所帮助


In other programming languages, to access attributes in of a class instance you just do:

print(Object.Attr)

and you do so in python as well, but how exactly you get a certain attribute at a certain index in a list of objects, normally i'd do this:

ListObj[i].attr

but the object is in a list. I want to access a specific index inside the list and pull the object attributes inside that index.

In Python, to access object attribute the syntax is

<object instance>.<attribute_name>

In your case, [index].

As Paul said, use dir(<object_instance>[index]) to get all attribute, methods associated object instance.

You can also use for loop to iterate over

for <obj> in <object_list>:
    <obj>.<attribute_name>

Hope it helps