0

Are there any advantages of declaring and initializing a subclass using a base class constructor method with parameters:

class BaseClass(object):
  def __init__(self, a, b):
    self.a = a
    self.b = b

class SubClass(BaseClass):
  def __init__(self, a, b, c, d):
    BaseClass.__init__(self, a, b)
    # or super().
    self.c = c
    self.d = d

newSubClassInstance = SubClass("one", "two", "three", "four")

Over not using parameters at all and just initializing as follows:

class BaseClass(object):
  def __init__(self):
    self.a = " "
    self.b = " "

class SubClass(BaseClass):
  def __init__(self):
    self.c = " "
    self.d = " "

newSubClassInstance = SubClass()
newSubClassInstance.a = "one"
newSubClassInstance.b = "two"
newSubClassInstance.c = "three"
newSubClassInstance.d = "four"

Or is it just up to how one intends to write and use this code within a program? I'm asking mostly about expectations though; would one be better over another if asked to create this particular task in Python, for example?

3
  • 1
    newSubClass should be newInstance because it’s an object of type SubClass and not a class. Other than that, both versions of the code achieve the same. If you’re familiar with private and protected class members in other programming languages (e.g. C++ or Java) then constructors make more sense, but for Python it doesn’t much matter either way. (Also, please call super().__init__() in your second example to ensure complete initialization.) Commented Mar 27, 2019 at 5:36
  • Okay, I made those edits to the question ... thank you, makes sense! Commented Mar 27, 2019 at 5:51
  • I would argue that there is lot of advantages of using base constructor. First is readability of this code, just create object with it's constructor and it's done. Second is that it is ensured that you provided all arguments, you won't forget to initialise something. Third is that you can do a lot of things in constructor, for example you can read configuration data from file. Using it gives you opportunity to do such operations without complicating usage of such class. Commented Mar 27, 2019 at 5:54

1 Answer 1

2

You should definitely instantiate and setup you class in the __init__ method. When the class is (created and) initilaized it should, if possible, be ready to be used. That is what one would expect from a class.

For some situations, e.g. when trying to mimic an immutable, the values should never be updated after the __init__() call. There is also a penality in updating after initialization, as an update may need to trigger other changes. Think of a GUI where one of the inputs is the size of a window being drawn. If that is set after the Window first appear, it has to be redrawn.

Also, your second approach is bad in another way; you do not even have the self.a and self.b after the __init__() call, as you never call BaseClass.__init__(). This you should always do when inheriting and overriding __init__().

You should also consider replacing your explicit call to BaseClass with a call to super() (as you have written as a comment) as that will handle multiple inheritance better.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.