[Tutor] constructor

Shurui Liu (Aaron Liu) shurui91 at gmail.com
Sun Apr 4 20:05:47 CEST 2010

I am studying about how to create a constructor in a Python program, I
don't really understand why the program print out "A new critter has
been born!" and "Hi.  I'm an instance of class Critter." twice. I
guess is because "crit1 = Critter()     crit2 = Critter()"  But I
don't understand how did computer understand the difference between
crit1 and crit2? cause both of them are equal to Critter(). Thank you!

# Constructor Critter
# Demonstrates constructors

class Critter(object):
    """A virtual pet"""
    def __init__(self):
        print "A new critter has been born!"

    def talk(self):
        print "\nHi.  I'm an instance of class Critter."

# main
crit1 = Critter()
crit2 = Critter()


raw_input("\n\nPress the enter key to exit.")

Shurui Liu (Aaron Liu)
Computer Science & Engineering Technology
University of Toledo

More information about the Tutor mailing list