如何在给定特定索引的对象列表中访问和对象属性? [英] How do i access and object attribute inside a list of objects given certain index?

查看:121
本文介绍了如何在给定特定索引的对象列表中访问和对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其他编程语言中,要访问类实例中的属性,您只需:

  print(Object.Attr) 

你也可以在python中这样做,但是你如何在某个索引处得到某个属性在一个对象列表中,通常我会这样做:

  ListObj [i] .attr 

但对象在列表中。我想访问列表中的特定索引并拉出该索引中的对象属性。

解决方案

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

 < object instance>。< attribute_name> 

在您的情况下,[index]。



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



您还可以使用for循环迭代

  for< ; OBJ>在< 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

这篇关于如何在给定特定索引的对象列表中访问和对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆