classes and __init__ question

Patrick Maupin pmaupin at gmail.com
Mon May 17 17:09:34 EDT 2010


On May 17, 3:55 pm, Alex Hall <mehg... at gmail.com> wrote:

> So what is a subclass compared to a class? Are you saying that what is
> passed to the class, so what is in the parentheses of the class, is
> really the superclass? If so, what is the advantage of doing this; why
> not just create a class that is not a sub? I thinking I am missing
> something elementary here. :)

A subclass can inherit methods and attributes from a base class.  This
is not necessary, but is sometimes useful.

> In Java, a class is an object. Is Python the same thing?

Yes, a class is an object.  A class's class is its "metaclass".

An instance of a class is also an object.

> Would you say
> that my dog class, for example, creates a new dog object when an
> instance is instantiated?

Yes. But the actual act of coding something like:

class foo(object):
    pass

Creates a class object, which is a subclass of the 'object' object,
and is an instance of the 'type' object.  Since Python is so dynamic,
you can easily determine this at the command prompt:

>>> class foo(object):
...     pass
...
>>> x = foo()
>>>
>>> type(x)
<class '__main__.foo'>
>>> type(foo)
<type 'type'>
>>>


Regards,
Pat



More information about the Python-list mailing list