如何从没有'__getitem__'属性的类对象获取参数?

0 投票
1 回答
576 浏览
提问于 2025-04-17 19:37

我正在使用 pyproj.Geod 来计算地理距离,我想用这个对象定义的地球直径来做其他计算。

当这个对象没有 __getitem__ 属性,并且不能通过索引来访问时,有没有办法从中提取一个值呢?

调用这个对象:

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

尝试通过参数名称来调用:

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

测试索引访问:

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

更新: 调用 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']

1 个回答

1

你是在找 getattr 这个内置函数吗?

撰写回答