Creating subclassess (newbie)

A

Adam

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
 
D

Duncan Booth

Adam said:
What is the best way of creating (coding) subclasses?
Alternatively, is there any good documentation on the web
for doing this?

Have you tried reading the Python tutorial? http://docs.python.org/tut if
you can't find it on your own machine. Try reading chapter 9: 'classes'
with special attention to 9.5 'inheritance'.

If you want coding examples you could look at Python's standard library.
There are plenty of examples of subclassing. For example, try CGIHTTPServer
and its associated documentation, htmllib or HTMLParser are other places
to look.

http://docs.python.org/lib/htmlparser-example.html shows an example of
subclassing the HTMLParser class to parse html.
 
L

Lonnie Princehouse

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'
main_bar
 
P

Piet van Oostrum

A> I have tried to send this to the tutor mailing list, but it
A> seems to be down at the moment.

A> I have a subclass I want to create- my intuition told me
A> that it would be done like this:

A> class MainClass:
A> class SubClass:
A> code...
A> subclassinstance = SubClass()
A> mainclassinstance = MainClass()

A> But it seems that this isn't going to work. I'm reading a
A> couple of Python books, but they don't seem to cover this
A> topic very well (I don't see any coding examples).

I can't imagine that the Python books don't tell this.

A> What is the best way of creating (coding) subclasses?
A> Alternatively, is there any good documentation on the web
A> for doing this?

I guess every Python introduction tells you:

class MainClass:
....

class SubClass(MainClass):
....

mainclassinstance = MainClass()
subclassinstance = SubClass()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top