1

I am trying to understand python classes:

I have the following case:

class parent:
    def __init__(self):
        self.a = 1
        self.b = 2
    def printFoo(self):
        print(self.a)
        
        
class child(parent):
    def createC(self, val): # Create a variable inside the derived class?
        self.c = val
    
    def printFoo(self): # overloaded function
        print(self.c)
        
a = parent()
b = child()

b.createC(3)
b.printFoo()
# Edited:
# I can even create variables as: child.d = 10

I would like to define a variable c and store it in the child class. Is it suitable to do this? Should I define an __init__ method to create the variable?

Best regards

2 Answers 2

1

Yes, you can/should definitely have __init__ in the derived classes. You just need to initialize the base class via super method. Here is the example according to your case.

class parent:
    def __init__(self):
        self.a = 1
        self.b = 2
    def printFoo(self):
        print(self.a)
        
        
class child(parent):
    def __init__(self):
        super().__init__() # initialize base class, pass any parameters to base here if it requires any
        self.c = someval
    # def createC(self, val): dont need these
    #    self.c = val
    
    def printFoo(self): # overloaded function
        print(self.c)

If your base class requires some parameters, you can do it like this

class parent:
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def printFoo(self):
        print(self.a)
        
        
class child(parent):
    def __init__(self, a, b, someval):
        super().__init__(a, b) # initialize base class, pass any parameters to base here if it requires any
        self.c = someval
    #def createC(self, val): dont need these
    #    self.c = val
    
    def printFoo(self): # overloaded function
        print(self.c)
Sign up to request clarification or add additional context in comments.

5 Comments

Hi thanks, I just noticed that I can even add variables from outside the class. something like child.c = 30.
Funnily I thought you had to call super().__init__() to initialise the base class, but I just tried the example code above and self.a and self.b were initialised correctly, in python 3.8.8.
@JohnM. try my 2nd example without super().__init__() and you'll know.
@ThomasJerry yes you can do it, but you should not do it like this as it is not a good practice.
It's clear the second example needs initialisation, but if the base constructor has no input parameters it seems to work OK. I think there is a strong best practice argument.
0

Do you want your variable to change ? You can simply put c inside your new child class, such as a static class variable (have a look here maybe).

class child(parent):
    c = 3

You can access it doing :

child.c

If you want to pass by the __init__() function and define the new variable when you define the class, use the super() method as @Ahmad Anis was suggested.

Comments

Your Answer

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