Passing parameter to base class constructor or using instance variable?

Posted by deamon on Stack Overflow See other posts from Stack Overflow or by deamon
Published on 2010-04-28T09:48:27Z Indexed on 2010/04/28 9:53 UTC
Read the original article Hit count: 198

Filed under:
|
|
|

All classes derived from a certain base class have to define an attribute called "path". In the sense of duck typing I could rely upon definition in the subclasses:

class Base:
    pass # no "path" variable here

def Sub(Base):
    def __init__(self):
        self.path = "something/"

Another possiblity would be to use the base class constructor:

class Base:
    def __init__(self, path):
        self.path = path

def Sub(Base):
    def __init__(self):
        super().__init__("something/")

What would you prefer and why? Is there a better way?

© Stack Overflow or respective owner

Related posts about python

Related posts about parameters