首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python有类的默认构造函数吗?

Python有类的默认构造函数吗?
EN

Stack Overflow用户
提问于 2017-03-19 13:28:42
回答 1查看 2.8K关注 0票数 4

我在问自己Python3.6是否有类的默认构造函数。所以我写了一些代码来测试这个。但我的问题比我开始测试的时候还多。

我试图理解为什么myCat在我的程序的第11行上构造ok,但是我无法解释为什么我的程序的第27行要求知道自己

如果你能帮忙,请回顾一下Python代码和输出.说得通吗?我制作了两个文件animal.py和an_test_program.py。

代码语言:javascript
复制
#!/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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-19 13:30:51

你什么都没做。您只创建了对animal类的另一个引用。

该类已经具有nameheightweight属性,因此print()语句可以访问这些属性并打印值。您还可以为这些类属性提供一个不同的值,因此myCat.name = 'Backstreet'也能工作。不过,这种变化也可以通过animal.name看到。

myCat.printvars是对您定义的方法的引用,但它是未绑定的;这里没有实例,只有类对象,因此没有什么可以将self设置为。在本例中,您可以显式地传递self的值,这就是myCat.printvars(myCat)工作的原因;您显式地将self设置为myCat,并且该类对象具有此操作所需的属性。

您仍然可以从myCat引用中创建实际实例:

代码语言:javascript
复制
an_actual_cat = myCat('Felix', 10, 15)

请记住,Python中的所有名称都是引用;可以通过赋值来对对象进行更多的引用:

代码语言:javascript
复制
foo = an.animal
bar = an.animal

现在,foobar都指向animal类。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42887152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档