Reputation: 475
I have a class abalone
with three attributes M
, I
and F
. Accessing them like so:
abalone.M
Out[81]:
array([[ 0.455 , 0.365 , 0.095 , ..., 0.2245, 0.101 , 0.15 ],
[ 0.35 , 0.265 , 0.09 , ..., 0.0995, 0.0485, 0.07 ],
[ 0.44 , 0.365 , 0.125 , ..., 0.2155, 0.114 , 0.155 ],
...,
[ 0.59 , 0.44 , 0.135 , ..., 0.439 , 0.2145, 0.2605],
[ 0.6 , 0.475 , 0.205 , ..., 0.5255, 0.2875, 0.308 ],
[ 0.71 , 0.555 , 0.195 , ..., 0.9455, 0.3765, 0.495 ]])
works splendidly. How would I go about iterating over them? My try
for item in [M, I, F]:
abalone.item
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-84-c9c2187c3451> in <module>()
----> 1 for item in [M, I, F]:
2 abalone.item
NameError: name 'M' is not defined
doesn't work unfortunately.
Edit: Trying
for item in ['M', 'I', 'F']:
abalone.item
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-86-b9d61335686f> in <module>()
1 for item in ['M', 'I', 'F']:
----> 2 abalone.item
AttributeError: 'Abalone' object has no attribute 'item'
does not work either.
Upvotes: 2
Views: 3774
Reputation: 341
Try this :
for attr, value in abalone.__dict__.items():
print attr,value
If it doesn't work then let me know.
Edited (according to change in problem):
l = ['M','I','F']
for attr, value in x.__dict__.iteritems():
if attr in l:
print attr,value
Here l will contain the subset of attributes you want to access.
Upvotes: 2