1
我有對象的像字典所以 {'name1':oject_instance_1,'name2':oject_instance_2,'name3':oject_instance_3}
在對象的我的類定義,我已經定義兩個__str__() method
和__repr__()
方法如下:上的對象的字典主叫STR
def __str__(self):
return('{0} pathway containing {1} genes, with a total sequence length of {2}'.format(self.id, len(self.genes), self.length))
def __repr(self):
return self.__str__
萬一這一點很重要self.id是一個字符串,self.genes是一個列表,self.length是一個int
問題是,當我去打印這本詞典,我得到:
{'pid1003': <Pathway.Pathway instance at 0x10169d680>, 'pid1002': <Pathway.Pathway instance at 0x10169d638>, 'pid1001': <Pathway.Pathway instance at 0x10169d5f0>}
,但打印在一個循環中像
for v in dict.values():
print(v)
工作正常。
任何想法爲什麼? 謝謝!
你的表示方法真的叫'__repr'(沒有第二組下劃線)? – 2012-07-26 13:33:48
另外,'__repr__'中的代碼是不是'return self .__ str __()'?事實上,返回一個方法對象似乎有點奇怪... – mgilson 2012-07-26 13:34:59
那真是令人尷尬。謝謝! :( – 2012-07-26 13:36:55