官網(wǎng)解釋:

作為一家“創(chuàng)意+整合+營(yíng)銷”的成都網(wǎng)站建設(shè)機(jī)構(gòu),我們?cè)跇I(yè)內(nèi)良好的客戶口碑。成都創(chuàng)新互聯(lián)提供從前期的網(wǎng)站品牌分析策劃、網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、創(chuàng)意表現(xiàn)、網(wǎng)頁(yè)制作、系統(tǒng)開(kāi)發(fā)以及后續(xù)網(wǎng)站營(yíng)銷運(yùn)營(yíng)等一系列服務(wù),幫助企業(yè)打造創(chuàng)新的互聯(lián)網(wǎng)品牌經(jīng)營(yíng)模式與有效的網(wǎng)絡(luò)營(yíng)銷方法,創(chuàng)造更大的價(jià)值。
object.__call__(self[, args...])
Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1,arg2, ...).
看得懂,但看不明白。。。。。真是硬傷。。。。
這兩天發(fā)文老是被審核,想不通,是不是我把廖某的全名打出來(lái)有人不爽????
為了更好理解,引用廖某的__call__程序與自己的__repr__ / __str__程序做比較:
#廖:__call__此程序的結(jié)果是輸出輸入的name
class Student1(object):
def __init__(self, name):
self.name = name
def __call__(self):
print('My name is %s.' % self.name)
h = Student1('MumU')
print 'liao:', h
#我:__repr__ / __str__ 此程序的結(jié)果也是輸出輸入的name
class Student2(object):
def __init__(self, name):
self.name = name
def __str__(self):
return 'Student2 name:%s'%self.name
__repr__ = __str__
l = Student2('U')
print 'me:', l運(yùn)行結(jié)果:
liao: <__main__.Student1 object at 0x7f5fea3108d0> me: Student2 name:U
嗯,為啥Student1會(huì)輸出這樣呢???
再換一個(gè)試法:程序Class都沒(méi)改,只是輸出語(yǔ)句改了(標(biāo)記:####)
#廖:__call__此程序的結(jié)果是輸出輸入的name
class Student1(object):
def __init__(self, name):
self.name = name
def __call__(self):
print('My name is %s.' % self.name)
print Student1('MumU') ####
#我:__repr__ / __str__ 此程序的結(jié)果也是輸出輸入的name
class Student2(object):
def __init__(self, name):
self.name = name
def __str__(self):
return 'Student2 name:%s'%self.name
__repr__ = __str__
print Student2('U') ####運(yùn)行結(jié)果:
liao: <__main__.Student1 object at 0x7f13dc36b8d0> me: Student2 name:U
嗯,還是一樣,想想也是,因?yàn)檫@輸出寫(xiě)法和之前的是等價(jià)的。。。。于是我就想,如果__call__是調(diào)用自己的函數(shù)的話,那么要用函數(shù)就得。。。在末尾加()號(hào)?!
再試:只改了廖的輸出語(yǔ)句(標(biāo)記:####)
#廖:__call__此程序的結(jié)果是輸出輸入的name
class Student1(object):
def __init__(self, name):
self.name = name
def __call__(self):
print('My name is %s.' % self.name)
print Student1('MumU')() ####
#我:__repr__ / __str__ 此程序的結(jié)果也是輸出輸入的name
class Student2(object):
def __init__(self, name):
self.name = name
def __str__(self):
return 'Student2 name:%s'%self.name
__repr__ = __str__
print Student2('U')運(yùn)行結(jié)果:
liao: My name is MumU. None me: Student2 name:U
敲你嗎,還真是函數(shù)。。。。
敲了這么多,蒙了也正常,換個(gè)普通的就懂了。
class Animal(object):
def __init__(self, name):
self.name = name
def run(self):
print '%s is running'%self.name
dog = Animal('dog')
print '當(dāng)你要查看class的屬性name時(shí):', dog.name
print '當(dāng)你要查看class的方法(函數(shù))時(shí):', dog.run()運(yùn)行結(jié)果:
當(dāng)你要查看class的屬性name時(shí): dog 當(dāng)你要查看class的方法(函數(shù))時(shí): dog is running None
查看屬性是不加()號(hào)的, 使用方法(函數(shù))時(shí)才需要。
至于為什么運(yùn)行末尾都有個(gè)None??
Because,
dog.run()已經(jīng)執(zhí)行了一次print 了,
def run(self): print '%s is running'%self.name
而
print '當(dāng)你要查看class的方法(函數(shù))時(shí):', dog.run()
再次執(zhí)行了一次,所以這個(gè)print 就只能輸出None了。
我真是太聰明了,哈哈哈哈哈
callable: 查看對(duì)象是否可調(diào)用,即是否為函數(shù)
print 'Student1 可調(diào)用??', callable(Student1('MumU'))
print 'Student2 可調(diào)用??', callable(Student2('U'))運(yùn)行結(jié)果:
Student1 可調(diào)用?? True Student2 可調(diào)用?? False
當(dāng)前名稱:pythonClass:面向?qū)ο蟾呒?jí)編程__call__&callable()
本文URL:http://www.yijiale78.com/article46/pdsieg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、關(guān)鍵詞優(yōu)化、網(wǎng)站導(dǎo)航、企業(yè)建站、網(wǎng)站維護(hù)、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)