Php Python';s相当于$this->$瓦名称
在PHP中,我可以执行以下操作:Php Python';s相当于$this->$瓦名称,php,python,Php,Python,在PHP中,我可以执行以下操作: $myVar = 'name'; print $myClass->$myVar; // Identical to $myClass->name 我希望在Python中实现这一点,但无法了解在Python中,它是如何使用getattr内置函数的 class Something( object ): def __init__( self ): self.a= 2 self.b= 3 x= Something(
$myVar = 'name';
print $myClass->$myVar;
// Identical to $myClass->name
我希望在Python中实现这一点,但无法了解在Python中,它是如何使用getattr内置函数的
class Something( object ):
def __init__( self ):
self.a= 2
self.b= 3
x= Something()
getattr( x, 'a' )
getattr( x, 'b' )
myvar = 'name'
//both should produce the same results
value = obj.name
value = getattr(obj, myvar)
您需要使用getattr内置函数
class Something( object ):
def __init__( self ):
self.a= 2
self.b= 3
x= Something()
getattr( x, 'a' )
getattr( x, 'b' )
myvar = 'name'
//both should produce the same results
value = obj.name
value = getattr(obj, myvar)