[Tutor] constructor
Shurui Liu (Aaron Liu)
shurui91 at gmail.com
Sun Apr 4 20:11:42 CEST 2010
Sorry, let me specify. I think comptuter will print out those two
sentences like this:
A new critter has been born!
Hi. I'm an instance of class Critter.
A new critter has been born!
Hi. I'm an instance of class Critter.
But the result I got from computer is like this:
A new critter has been born!
A new critter has been born!
Hi. I'm an instance of class Critter.
Hi. I'm an instance of class Critter.
On Sun, Apr 4, 2010 at 2:05 PM, Shurui Liu (Aaron Liu)
<shurui91 at gmail.com> wrote:
> 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()
>
> crit1.talk()
> crit2.talk()
>
> raw_input("\n\nPress the enter key to exit.")
>
>
> --
> Shurui Liu (Aaron Liu)
> Computer Science & Engineering Technology
> University of Toledo
> 419-508-1228
>
--
Shurui Liu (Aaron Liu)
Computer Science & Engineering Technology
University of Toledo
419-508-1228
More information about the Tutor
mailing list