0

I am using pyproj.Geod to calculate geographic distances, and I would like to use the diameter of the earth defined by the object for another calculation.

When there is no __getitem__ attribute and the object is not indexable, is there a way of extracting a value from it?

Calling the object:

import pyproj
g = pyproj.Geod(ellps='WGS84') # Use WGS84 ellipsoid

Trying to call it by parameter name:

print g['a'] # the diameter parameter
TypeError: 'Geod' object has no attribute '__getitem__'

Testing indexing:

print g[0] 
TypeError: 'Geod' object does not support indexing

Update: Calling print dir(g):

print dir(g)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_fwd', '_inv', '_npts', 'a', 'b', 'es', 'f', 'fwd', 'initstring', 'inv', 'npts', 'sphere']
ryanjdillon
  • 17,658
  • 9
  • 85
  • 110

1 Answers1

1

Are you looking for the getattr built-in function?

Michael Wild
  • 24,977
  • 3
  • 43
  • 43
  • That was it! Using `dir(g)` to verify the name of the attribute first, I then did `print getattr(g,'a')`, which gave me what I was looking for. Thanks Martijn and Michael! – ryanjdillon Mar 19 '13 at 11:33
  • 1
    But then, that would be equivalent to `print g.a`. You only need `getattr` in cases, where you have the name of the property as a string and you need to dynamically retrieve it. If you know already that it's called `a`, just use the dot notation. – Michael Wild Mar 19 '13 at 11:34