classes and __init__ question

Ethan Furman ethan at stoneleaf.us
Mon May 17 18:01:35 EDT 2010


Alex Hall wrote:
> Hi all,
> I am a bit confused about classes. What do you pass a class, since all
> the actual information is passed to __init__? For example, say you
> have a dog class. The dog object has a name, a size, and a color. I
> believe you would say this:
> 
> class dog():
>  def __init__(self, name, size, color):
>   self.name=name
>   self.size=size
>   self.color=color

Here you have defined a dog class (you have not called it, yet).


> What, then, gets passed to the class constructor?
> class dog(whatGoesHere?):

whatGoesHere = any parent classes (the constructor is *not* being called 
yet)

parent classes can be used when you want what they have, but need to 
either add more, or change some, of the methods/attributes that the 
parent has.

> Sometimes I see things passed to this. For example, if you create a
> class for a wxPython frame, you will say:
> class myapp(wx.App):

wx.App is not being passed, per se, rather Python is being told that 
myapp is based on wx.App, and will have all the methods/attributes that 
wx.App has, plus whatever else follows.

So far, all that has been done is to _define_ classes.

> class contact():
>  def __init__(self, name, email, status, service):
>   self.name=name
>   self.email=email
>   self.status=status
>   self.service=service
> 
> Here, I do not pass anything to the class, only to __init__. What is going on?

You have defined a class -- nothing more, nothing less.

When you actually call the class is when any necessary items are passed 
to the constructor (__init__):

a_dog = dog('Ralph', 'big', RED)

this_app = myapp()

fred = contact('Fred Flinstone','f1 at flinstone.gv','active','exemplary')

Hope this helps.

~Ethan~



More information about the Python-list mailing list