<p>我有下面的类,我在声明'constants'将一个值映射到另一个值时使用它。python中有没有实现一个默认方法,以便在没有特定方法的情况下引用这个类的实例时,执行一个默认方法</p>
<pre><code>class FieldMap:
def __init__(self, old_field, new_field):
self._fld_map = (old_field, new_field)
def old_fld(self):
return self._fld_map[0]
def new_fld(self):
return self._fld_map[1]
SEC_ID = FieldMap('value','price')
SEC_NAME = FieldMap('entity_name', 'security_name')
SEC_TICKER = FieldMap('entity_attr', 'ticker')
#Edit: updating example to provide a real example of what i want to achieve
dict = {}
dict['price'] = 1234
print dict[SEC_ID] <--would like this to print out the value 1234 because ideally, the default method would call new_fld() and return 'price'
</code></pre>