python - 多態(tài)調(diào)用方法時(shí)卻顯示bound method...
問(wèn)題描述
class Programer(object):
hobby=’play computer’def __init__(self,name,age,weight): self.name=name self._age=age self.__weight=weightdef self_intro(self): print ’my name is %s nI am %s years oldn’ % (self.name, self._age)
class BackendProgramer(Programer):
def __init__(self,name,age,weight,language): super(BackendProgramer,self).__init__(name,age,weight) self.language = languagedef self_intro(self): print ’my name is %s nmy favourite language is %sn’ % (self.name, self.language)
def intro(f):
if isinstance(f,Programer): print f.self_intro
if __name__==’__main__’:
prog = Programer(’Albert’,25,’80’)back_prog = BackendProgramer(’Alex’,23,’80’,’Ruby’)intro(prog)intro(back_prog)
運(yùn)行結(jié)果是:<bound method Programer.self_intro of <__main__.Programer object at 0x02505330>><bound method BackendProgramer.self_intro of <__main__.BackendProgramer object at 0x02505370>>
請(qǐng)問(wèn)為什么運(yùn)行結(jié)果不是my name is ...
問(wèn)題解答
回答1:因?yàn)槟阃浾{(diào)用了
def intro(f): if isinstance(f,Programer):print f.self_intro() # 沒(méi)有()只是函數(shù)對(duì)象而已, 加了()才是調(diào)用
相關(guān)文章:
1. matplotlib - python函數(shù)的問(wèn)題2. javascript - history.replaceState()無(wú)法改變query參數(shù)3. java - 是否類(lèi) 類(lèi)型指針、引用作為形參 ,函數(shù)結(jié)束不會(huì)自動(dòng)析構(gòu)類(lèi)?4. angular.js - angular ng-class里面的引號(hào)問(wèn)題5. 在mac下出現(xiàn)了兩個(gè)docker環(huán)境6. docker start -a dockername 老是卡住,什么情況?7. mysql無(wú)法添加外鍵8. javascript - react 中綁定事件和阻止事件冒泡9. JavaScript事件10. javascript - es6將類(lèi)數(shù)組轉(zhuǎn)化成數(shù)組的問(wèn)題
