2014-12-03 29 views

回答

2

只需追加另一個點即可獲得其他類屬性。這應做到:

class dog: 
    def __init__(self,x,y): 
     self.x=x 
     self.y=y 
    def print_a(self): 
     print self.x.a 
     print self.y.a 

您也可以使用getattr打印由名稱屬性:

def print_by_name(self, attr_name): 
     print getattr(self.x, attr_name) 
     print getattr(self.y, attr_name) 

    # e.g. print_by_name(a) would print self.x.a and self.y.a