如何在python

时间:2019-02-04 21:30:44

标签: python-2.7 properties

我想使用字符串'_height'访问其属性中定义的Player类中_height的值,如果可能的话,不进行字符串操作。

class Player:
    def __init__(self, height):
        self._height = SyncVar(height)

    @property
    def height(self):
        return self._height.v

class SyncVar(object):
    def __init__(self, v):
        self.v = v

 p = Player(5)
 var_name = '_height'
 print 'The value of _height is %s' + str(p.__dict__[var_name])
 # Wanted: The value of _height is 5
 # What happens: The value of _height is <Main.SyncVar object at 0x0000000002DF8198>

0 个答案:

没有答案