0

I am still new to OOP.

In an instance of a child class I want to use a parameter that is set when the parent class is initialized:

class parent():
    def __init__(self, config):
        self._config = config

class child(parent):  
    print self._config

x = "Hello"
c = child(x)

This does not work, an error is thrown since self is unknown. Do I really need to copy the complete __init__ block from the parent class every time? I saw that in some cases an initialization can be triggered without an __init__ block:

class parent():
    def __init__(self, config):
        self._config = config

class child(parent):  
    # Nonsense since not itarable, but no error anymore
    def next(self):
        print self._config

x = "Hello"
c = child(x)

Although that does not work, it still throws no error. Is there any short method to initialize the parent class in the child class or to get all parameters from the parent?

aldorado
  • 4,394
  • 10
  • 35
  • 46
  • 1
    using `print self._config` likely isn't doing what you think it's doing, and is a poor choice to illustrate inheritance. It's highly unusual to put anything but assignment statements where you have the print statement in the first example. – Bryan Oakley May 24 '16 at 16:32
  • 1
    You're probably looking for calling `parent` class's `__init__()` method. [This](http://stackoverflow.com/q/904036/1377864) seems to be relevant. – Yaroslav Admin May 24 '16 at 16:32
  • Yes, it was about accessing the parameters, not about the printing. Your link helps – aldorado May 24 '16 at 16:40

3 Answers3

1
x = "Hello"
c = child(x)

This code does create an instance of child, with _config = "Hello".

However, that's all it does. It does not print the value of _config, which you seem to be expecting.

If you want the __init__ function to print the value of self._config, you'll have to add code to make it do that.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
1

You can simply invoke the parent's __init__, which is common practice:

class child(parent):
    def __init__(self, config):
        super(child, self).init(config)
        print self._config
Prune
  • 76,765
  • 14
  • 60
  • 81
  • If I have multiple arguments, not only "config", do I need to rewrite them all in super() ? Or is there a more generic way? – aldorado May 24 '16 at 16:43
  • 1
    You need to pass them all; the child *class* inherits the attributes of the parent; this does not mean that the child class *definition* inherits the syntax of the parent's definition. It's possible to convert individual arguments to a list (e.g. *args), if that works better for you. – Prune May 24 '16 at 16:45
1
class parent:
    def __init__(self, config):
        self._config = config

class child(parent):

    def __init__(self,config):
        parent.__init__(self,config)

    def next(self):
        print self._config

x = child("test")
x.next() # prints test

All the parameters to be passed to parent must be passed to child to initialize the parent inside the __init__ of child

Harwee
  • 1,601
  • 2
  • 21
  • 35
  • This was the code I finally used. I did not fully understand though, if rather to use super or the parent class is the more pythonic way of programming. – aldorado May 25 '16 at 09:23
  • When you inherit a parent class into child class you need to initialize the parent class for the child to get characteristics of parent. So when initializing the child the first thing you do do is initialize the parent by passing the arguments which the parent require. – Harwee May 25 '16 at 09:36