Wednesday, 21 August 2013

get attribute with getattr from a object in python

get attribute with getattr from a object in python

I have an object p (liblas.point.Point) with several attributes
pattr = {
"r": 'return_number',
"n": 'number_of_returns',
"s": 'get_point_source_id()',
"e": 'flightline_edge',
"c": 'classification',
"a": 'scan_angle',
}
mylist = ["r", "n", "e", "c", "a"]
for letter in mylist:
print getattr(p, pattr[letter])
1
3
0
1
-23
I have a problem with get_point_source_id() where
p.get_point_source_id()
20
but when i use getattr i got this message
getattr(p, pattr["s"])
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py",
line 2721, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-81-bbad74aa3829>", line 1, in <module>
getattr(p, pattr["s"])
AttributeError: 'Point' object has no attribute 'get_point_source_id()'

No comments:

Post a Comment