-3

I have the parent method:

 def move(self,dx,dy):
     self.x += dx
     self.y += dy

which will move a ex: Point(1,1) move(2,3) -> Point(3,4)

I have a child function which needs to contain that same method, how do I call it?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
andrchr
  • 1
  • 2

2 Answers2

0

If your child class function has a different name, just use call self.move(argx, argy). All methods are inherited along to be available on the instance.

If your child method has the same name (and thus overrides the parent method), you can use super(ChildClass, self).move(argx, argy) to call it. If you are using Python 3, you can omit the arguments to super() altogether and just use super().move(argx, argy).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Your code should look like this:

class A(object):
    def move(self, dx, dy):
        self.x += dx
        self.y += dy


class B(A):
    def foo(self, dx, dy):
        self.move(dx, dy)
dursk
  • 4,435
  • 2
  • 19
  • 30