Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameter to base class constructor or using instance variable?

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/")

I use Python 3.1.

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

like image 667
deamon Avatar asked Apr 28 '10 09:04

deamon


People also ask

Can we pass parameters to base class constructor?

To pass arguments to a constructor in a base class, use an expanded form of the derived class' constructor declaration, which passes arguments along to one or more base class constructors. Here, base1 through baseN are the names of the base classes inherited by the derived class.

How do you pass parameters to a constructor?

Arguments are passed by value. When invoked, a method or a constructor receives the value of the variable passed in. When the argument is of primitive type, "pass by value" means that the method cannot change its value.

Can we pass parameters to base?

To pass in parameter values, simply append them to the query string at the end of the base URL.

Can we pass parameters to base class constructor Mcq?

Can we pass parameters to base class constructor though derived class or derived class constructor? Explanation: Yes, we pass parameters to base class constructor though derived class or derived class constructor.


1 Answers

In Python 3.0+:
I would go with a parameter to the base class's constructor like you have in the second example. As this forces classes which derive from Base to provide the necessary path property, which documents the fact that the class has such a property and that derived classes are required to provide it. Without it, you would be relying on this being stated (and read) somewhere in your class's docstrings, although it certainly does help to also state in the docstring what the particular property means.

In Python 2.6+:
I would use neither of the above; instead I would use:

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

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

In other words, I would require such a parameter in the base class's constructor, because it documents the fact that all such types will have/use/need that particular parameter and that the parameter needs to be provieded. However, I would not use super() as super is somewhat fragile and dangerous in Python, and I would also make Base a new-style class by inheriting from object (or from some other new-style) class.

like image 180
Michael Aaron Safyan Avatar answered Oct 16 '22 23:10

Michael Aaron Safyan