python 遍历属性值问题

梁小白 2012-06-08 10:00:15
A是一个对象,有a,b,c三个属性可以用A.a,A.b,A.c 访问,我想遍历A的这三个属值怎么写啊
for x in dir(A):
print A.x?
这个x如何替换为A的相应属性a,b,c
...全文
4586 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
DevinXie 2014-03-28
  • 打赏
  • 举报
回复
引用 1 楼 bugs2k 的回复:
class AX:
    def __init__(self):
        self.x = 1
        self.y = 2.0
        self.z = 'hello'

if __name__ == '__main__':
    a = AX()
    for key in a.__dict__:
        print key, ':', a.__dict__[key]
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>> 
y : 2.0
x : 1
z : hello
>>> 
这个最好。

# -*- coding:utf-8

import sys
from Tkinter import *

class attrs:
    def __init__(self,**kw):
        self.__dict__.update(kw)
    def update(self,**kw):
        self.__dict__.update(kw)

obj_attr=attrs(alerm='red',warning='yellow',normal='green')

def exitnow():
    sys.exit()

def test():
    obj_attr.update(good='green',bad='red')

    print obj_attr.normal
    print obj_attr.bad
    lst = dir(obj_attr)
    for key in obj_attr.__dict__:
        print "-----------------__dict__----------------------------"
        print key,":",obj_attr.__dict__[key]
    for each in lst:
        if each[0:2] != '__' and each != 'update':
            print "---------------------------------------------"
            print each,eval("obj_attr." + str(each))
            print "---------------------------------------------"
            print each,getattr(obj_attr,str(each))
        #print each


if __name__=='__main__':

    

    win = Tk()
    win.title("Main")
    win.geometry('300x100')
    bttn_test = Button(win,text='Test',command=test)
    bttn_test.pack(expand=YES,fill=BOTH)

    bttn_exit = Button(win,text='Exit',command=exitnow)
    bttn_exit.pack(expand=YES,fill=BOTH)

    mainloop()

angel_su 2012-06-08
  • 打赏
  • 举报
回复
for x in dir(A):
print getattr(A, x)

实例有一大堆内建或继承而来的属性,不分门别类直接遍历处理的情况几乎没有吧,还是按4楼说的,把你关注的东西用个字典还是啥的关联一下...
panghuhu250 2012-06-08
  • 打赏
  • 举报
回复
虽然python可以做到你的要求,但也许这三个属性更适合放到一个dictionary里。
Gloveing 2012-06-08
  • 打赏
  • 举报
回复
for x in dir(A):

print eval("A." + str(x) )
Gloveing 2012-06-08
  • 打赏
  • 举报
回复
for x in dir(A):

print eval("A" + str(x) )
bugs2k 2012-06-08
  • 打赏
  • 举报
回复
class AX:
def __init__(self):
self.x = 1
self.y = 2.0
self.z = 'hello'

if __name__ == '__main__':
a = AX()
for key in a.__dict__:
print key, ':', a.__dict__[key]


Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
y : 2.0
x : 1
z : hello
>>>
梁小白 2012-06-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

for x in dir(A):
print getattr(A, x)

实例有一大堆内建或继承而来的属性,不分门别类直接遍历处理的情况几乎没有吧,还是按4楼说的,把你关注的东西用个字典还是啥的关联一下...
[/Quote]

for x in dir(A):
print getattr(A, str(x))
这样就可以了,3KS

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧