Newbie Question: Abstract Class in Python

Discussion in 'Python' started by Kevin Bass, Jul 7, 2003.

  1. Kevin Bass

    Kevin Bass Guest

    Hello:

    I am new to Python and want to know how to implement an abstract class? I
    have read through different books and I have no found this information.
    Thanks!

    Kevin
    Kevin Bass, Jul 7, 2003
    #1
    1. Advertising

  2. Kevin Bass

    Aahz Guest

    In article <5U2Oa.239646$>,
    Kevin Bass <> wrote:
    >
    >I am new to Python and want to know how to implement an abstract
    >class? I have read through different books and I have no found this
    >information. Thanks!


    Simple answer: you don't. Python doesn't really have that concept. If
    you tell us what you're trying to do, we can explain how to do that in
    Python.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "Not everything in life has a clue in front of it...." --JMS
    Aahz, Jul 7, 2003
    #2
    1. Advertising

  3. Kevin Bass wrote:

    > I am new to Python and want to know how to implement an abstract
    > class? I
    > have read through different books and I have no found this
    > information.
    > Thanks!


    There's no builtin way to do that in Python. Typically if I'm doing
    something where I want to make a class that yells at me if I 1.
    accidentally instantiate it directly or 2. fail to override one of its
    methods in a subclass, I do something like:

    class AbstractSomething:
    def __init__(self):
    if self.__class__ is AbstractSomething:
    raise NotImplementedError

    def aMethod(self, ...):
    raise NotImplementedError

    def anotherMethod(self, ...):
    raise NotImplementedError

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ They love too much that die for love.
    \__/ (an English proverb)
    Erik Max Francis, Jul 7, 2003
    #3
  4. Kevin Bass

    Ben Finney Guest

    On Mon, 07 Jul 2003 00:42:27 GMT, Kevin Bass wrote:
    > I am only looking for an answer about creating an abstract class in
    > Python and not a solution to a problem. I am looking for a way to
    > stop class instantiation of a class. This should be a generic
    > approach.


    Python, in general, doesn't try to stop the programmer doing things, the
    way many other languages do. This is known in the community as the
    "we're all consenting adults" philosophy.

    If you can tell us why you'd want to prevent class instantiation, we can
    discuss that.

    --
    \ "It's a good thing we have gravity or else when birds died |
    `\ they'd just stay right up there. Hunters would be all |
    _o__) confused." -- Steven Wright |
    http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
    Ben Finney, Jul 7, 2003
    #4
    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. Matthias Kaeppler
    Replies:
    1
    Views:
    420
    R.F. Pels
    May 22, 2005
  2. Sameer
    Replies:
    4
    Views:
    573
    Roedy Green
    Aug 31, 2005
  3. Uzytkownik
    Replies:
    3
    Views:
    578
    Uzytkownik
    Apr 3, 2005
  4. Iyer, Prasad C

    Abstract Methods & Abstract Class

    Iyer, Prasad C, Oct 20, 2005, in forum: Python
    Replies:
    0
    Views:
    526
    Iyer, Prasad C
    Oct 20, 2005
  5. Replies:
    4
    Views:
    801
    Rolf Magnus
    May 17, 2006
Loading...

Share This Page