2017-03-19 42 views
2

我在問自己Python3.6是否有類的默認構造函數。所以我寫了一些代碼來測試這個。但是當我開始測試時,我留下了更多的問題。Python是否有類的默認構造函數?

我試圖理解爲什麼myCat建在我的程序的第11行確定,但我無法解釋爲什麼我的程序的第27行要求將自己知道

請,如果你能幫助,請閱讀此Python代碼和輸出......它有道理嗎? 我做了2個文件animal.py和an_test_program.py。

#!/usr/bin/env python3 
#animal.py file ============================== 
class animal: 
    name = "" 
    height = 0 
    weight = 0 

    def __init__(self, name, height, weight): 
     self.name = name 
     self.height = height 
     self.weight = weight 

    def printvars(self): 
     print(self.name) 
     print(self.height) 
     print(self.weight) 


#!/usr/bin/env python3 
#an_animal_program.py ============================== 
import animal as an 

#Instantiate a variable of type animal using the special constructor of the class 
myDog = an.animal('Capitan', 30, 70) 
print('myDog\'s name is ' + myDog.name) 
myDog.printvars() 

#now play around with Python and discover some things... 
#Instantiate a variable of type animal without using the special constructor 
myCat = an.animal 
print('myCat\'s name is ' + myCat.name) 
print('myCat\'s height is ' + "%d" % (myCat.height)) 
print('myCat\'s weight is ' + "%d" % (myCat.weight)) 

#notice everything in myCat is empty or zero 
#thats because myCat is not initialized with data 
#none-the-less myCat is a viable object, otherwise Python would puke out myCat 

myCat.name = 'Backstreet' 
print('myCat\'s name is ' + myCat.name) # now myCat.name has data in it 
    #Ouch... this next line produces an error: 
    #myCat.printvars() 
    #"an_test_program.py", line 21, in <module> 
    # myCat.printvars() 
    #TypeError: printvars() missing 1 required positional argument: 'self' 
myCat.printvars(myCat) #ok, I'm rolling with it, but this is wierd !!! 

#oh well, enough of the wierdness, see if the myCat var can be 
#reinstantiated with an animal class that is correctly constructed 
myCat = an.animal('Backstreet', 7, 5) 
myCat.printvars() 

#I'm trying to understand why myCat constructed ok on line 11 
#But I can't explain why line 27 demanded to know itself 
#the output is: 
# RESTART: an_test_program.py 
#myDog's name is Capitan 
#Capitan 
#30 
#70 
#myCat's name is 
#myCat's height is 0 
#myCat's weight is 0 
#myCat's name is Backstreet 
#Backstreet 
#0 
#0 
#Backstreet 
#7 
#5 
+0

要迂迴,'.__ init__'就是實例_initializer_。實例構造函數被稱爲'.__ new__'。類從'object'繼承'.__ new__'方法,並且通常不需要覆蓋它,雖然這當然是可以的。 –

+0

謝謝。這就說得通了。 – MrEmanLOL

+0

您可能會發現這篇文章有幫助:[關於Python名稱和值的事實和神話](http://nedbatchelder.com/text/names.html),這是由SO老手Ned Batchelder編寫的。另請參閱[其他語言具有「變量」,Python具有「名稱」](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables)簡要總結,用可愛的圖表。 –

回答

3

您沒有構建任何東西。您只創建了animal課程的另一個參考。

該類已有name,heightweight屬性,因此print()語句可以訪問這些屬性並打印值。你也可以給這些類屬性一個不同的值,所以myCat.name = 'Backstreet'的作品也是。但是,通過animal.name也可以看到此更改。

myCat.printvars是所定義的方法的引用,但是它是未結合的;這裏沒有實例,只有一個類對象,所以沒有什麼可以設置self。在這種情況下,您可以明確地傳遞值self,這就是爲什麼myCat.printvars(myCat)有效;明確設置selfmyCat,並再次該類對象已爲此所需的屬性來工作..

您仍然可以從myCat參考創建實際實例:

an_actual_cat = myCat('Felix', 10, 15) 

只要記住,所有的名字Python是引用;你總是可以通過分配使對象更多的參考資料:

foo = an.animal 
bar = an.animal 

現在無論foobar還指出,animal類。

+0

完美!謝謝。我沒有認識到我引用了類對象並引用了它的內部函數。現在最清楚代碼真的在做什麼。 – MrEmanLOL

相關問題