-3

Lets say I have the following

 class cls1():   
    def __init__(self, num):  
        self.num = num  
    def square(self):  
        return self.num*self.num 

Then I create a subclass.

class cls2(cls1):
    k1 = super().square(self)

Basically I want to use the square function of the parent class. But this is giving an error RuntimeError: super(): no arguments
If I directly use the square function as following

class cls2(cls1):
        k1 = square(self)

This gives an error NameError: name 'square' is not defined .
I don't understand what is the problem. This is what inheritance means right? cls2 should automatically take all the data and procedural attributes from cls1. So why is this not working?

idpd15
  • 448
  • 2
  • 5
  • 22
  • You said the answer cls2 automatically inherits everything you don't need to add manually or if you want to use square with a different name you can use `k1 = self.square`. – Attila Bognár Dec 11 '18 at 08:41
  • should be `self.num` in the `square` method of `cls1`. And likewise your last snippet is failing because it should be `self.square`. (There's no need to call `super` here, it's just basic inheritance.) – Robin Zigmond Dec 11 '18 at 08:41
  • Is this `k1 = ...` line at `class` level or inside a function? – user2390182 Dec 11 '18 at 08:43
  • @schwobaseggl This is at the class level. I am just trying to understand how to use the function from the super class in a sub class – idpd15 Dec 11 '18 at 08:44
  • 1
    The main problem is that you're trying to call the *instance method* `square` without having an *instance* (you're still in the *class definition* stage there, you don't have an instance of the class yet). Why do you need to call `square` *there*? – deceze Dec 11 '18 at 08:47
  • @deceze What is the problem with using square? Let's say I want to save a class variable k1 which stores the square of the num . How can I do that? – idpd15 Dec 11 '18 at 08:51
  • Again, there's a difference between *instance methods*, which need an instance like `cls1().square()`, and *class methods*, which can be called on the class directly like `cls1.square()`. Since `square` **depends** on `self.num`, it's an instance method, and you cannot call it without an instance. It's unclear how you *expect* this to work at that point. – deceze Dec 11 '18 at 08:52
  • @deceze Thanks a lot for your patience in explaining. Seems like asking a dumb question(for others, not for me! - for me it's a genuine question. I am still learning the basics) gets you all the downvotes! – idpd15 Dec 11 '18 at 08:59
  • The downvotes are likely because it's unclear how you expect this to work, and it's therefore [not a super useful question for others to find in the future.](https://meta.stackoverflow.com/a/253230/476) – deceze Dec 11 '18 at 09:01

3 Answers3

1

You need to do nothing.
Super() is just needed to execute a parent's class method, although your definition of cls2 was not correct:

class cls1():   
    def __init__(self, num):  
        self.num = num  
    def square(self):  
        return self.num * self.num


class cls2(cls1):
    pass


c1 = cls1(5)
print ('C1:', c1.square())

c2 = cls2(8)
print ('C2:', c2.square())

Prints:

('C1:', 25)
('C2:', 64)
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
1

As has been stated, you don't need to do anything if you just want the square method to be present, it is inherited automatically. If you want a method k1 that does the square action, you can do:

class cls2(cls1):
    k1 = cls1.square
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

This could be what you looking for :

class cls1():
    def __init__(self, num):
        self.num = num

    def square(self):
        return self.num * self.num


class cls2(cls1):
    def __init__(self, num):
        super(cls2, self).__init__(num)

    def square(self):
        return super(cls2, self).square()


cls = cls2(3)
print(cls.square())

i think the code is self explainatory . hope that helps

Midhun Mohan
  • 552
  • 5
  • 18
  • You should not be passing any arguments to `super`, just `super().foo()` will do just fine. – deceze Dec 11 '18 at 08:49
  • Why do I need to define a square method in cls2? Can't I directly use the method in cls1. You are defining a method in cls2 using the method in cls1 – idpd15 Dec 11 '18 at 08:53
  • @idpd15 Yes, this is redundant and superfluous here. I think it's just supposed to illustrate when you might use `super` (when you want to access the parent's implementation of a method). – deceze Dec 11 '18 at 08:54
  • @Midhun Yes, you *can*, but you don't need to and therefore shouldn't: https://stackoverflow.com/a/10483204/476 – deceze Dec 11 '18 at 08:56
  • it is just a illustration super method usage – Midhun Mohan Dec 11 '18 at 08:56