Creating subclassess (newbie)
Lonnie Princehouse
fnord at u.washington.edu
Mon Jun 21 13:34:30 EDT 2004
Typically, "subclass" refers to a derived class. It looks like you've
interpreted it in a different way, to mean a class that is a member of
another class? Interesting =)
subclass example-
class MainClass:
def foo(self):
return 'main_foo'
def bar(self):
return 'main_bar'
class SubClass(MainClass):
def foo(self):
return 'sub_foo'
>>> x = SubClass()
>>> print x.foo()
sub_foo
>>> print x.bar()
main_bar
Adam <adamc at linuxmail.org> wrote in message news:<20040621153854.14f10a9f at debian>...
> I have tried to send this to the tutor mailing list, but it
> seems to be down at the moment.
>
> I have a subclass I want to create- my intuition told me
> that it would be done like this:
>
> class MainClass:
> class SubClass:
> code...
> subclassinstance = SubClass()
> mainclassinstance = MainClass()
>
> But it seems that this isn't going to work. I'm reading a
> couple of Python books, but they don't seem to cover this
> topic very well (I don't see any coding examples).
>
> What is the best way of creating (coding) subclasses?
> Alternatively, is there any good documentation on the web
> for doing this?
>
> Thanks in advance.
>
> Adam
More information about the Python-list
mailing list