Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python have a default constructor for classes?

I was asking myself whether Python3.6 had a default constructor for classes. So I wrote some code to test this. But I'm left with more questions than when I started testing this.

I'm trying to understand why myCat constructed ok on line 11 of my program, yet I can't explain why line 27 of my program demanded to know itself

Please, if you can help, review this Python code and the output... does it make sense? I made 2 files animal.py and 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
like image 245
MrEmanLOL Avatar asked Oct 20 '25 02:10

MrEmanLOL


1 Answers

You didn't construct anything. You only created another reference to the animal class.

That class has name, height and weight attributes already, so the print() statements can access those attributes and print the values. You can also give those class attributes a different value, so myCat.name = 'Backstreet' works too. This change would also be visible through animal.name however.

myCat.printvars is a reference to the method you defined, but it is unbound; there is no instance here, only a class object, so there is nothing to set self to. You can pass in a value for self explicitly in this case, which is why myCat.printvars(myCat) works; you explicitly set self to myCat, and again that class object has the required attributes for this to work..

You can still create actual instances from the myCat reference:

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

Just remember, all names in Python are references; you can always make more references to objects by assigning:

foo = an.animal
bar = an.animal

Now both foo and bar also point to the animal class.

like image 145
Martijn Pieters Avatar answered Oct 21 '25 16:10

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!