Creating subclassess (newbie)

Discussion in 'Python' started by Adam, Jun 21, 2004.

  1. Adam

    Adam Guest

    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
    Adam, Jun 21, 2004
    #1
    1. Advertising

  2. Adam

    Peter Hansen Guest

    Adam wrote:

    > 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()


    Have you followed through the tutorial in the documentation?
    The section on classes covers inheritance in a basic way.
    See
    http://www.python.org/doc/current/tut/node11.html#SECTION0011500000000000000000

    -Peter
    Peter Hansen, Jun 21, 2004
    #2
    1. Advertising

  3. Adam

    Duncan Booth Guest

    Adam <> wrote in news:20040621153854.14f10a9f@debian:

    > 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.
    Duncan Booth, Jun 21, 2004
    #3
  4. 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 <> wrote in message news:<20040621153854.14f10a9f@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
    Lonnie Princehouse, Jun 21, 2004
    #4
  5. >>>>> Adam <> (A) wrote:

    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()
    --
    Piet van Oostrum <>
    URL: http://www.cs.uu.nl/~piet [PGP]
    Private email:
    Piet van Oostrum, Jun 23, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Parker
    Replies:
    3
    Views:
    559
    Hans Kesting
    Jun 25, 2004
  2. JPH
    Replies:
    1
    Views:
    468
  3. Ravi Ambros Wallau
    Replies:
    0
    Views:
    4,085
    Ravi Ambros Wallau
    Jun 1, 2005
  4. Enes Kabacaoglu

    NEWBIE needs helps about creating links

    Enes Kabacaoglu, Dec 25, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    333
    Nathan Sokalski
    Dec 30, 2005
  5. Ravi Ambros Wallau
    Replies:
    0
    Views:
    305
    Ravi Ambros Wallau
    Jun 1, 2005
Loading...

Share This Page