Asked  7 Months ago    Answers:  5   Viewed   37 times

When creating a simple object hierarchy in Python, I'd like to be able to invoke methods of the parent class from a derived class. In Perl and Java, there is a keyword for this (super). In Perl, I might do this:

package Foo;

sub frotz {
    return "Bamf";
}

package Bar;
@ISA = qw(Foo);

sub frotz {
   my $str = SUPER::frotz();
   return uc($str);
}

In Python, it appears that I have to name the parent class explicitly from the child. In the example above, I'd have to do something like Foo::frotz().

This doesn't seem right since this behavior makes it hard to make deep hierarchies. If children need to know what class defined an inherited method, then all sorts of information pain is created.

Is this an actual limitation in python, a gap in my understanding or both?

 Answers

90

Use the super() function:

class Foo(Bar):
    def baz(self, arg):
        return super().baz(arg)

For Python < 3, you must explicitly opt in to using new-style classes and use:

class Foo(Bar):
    def baz(self, arg):
        return super(Foo, self).baz(arg)
Tuesday, June 1, 2021
 
Saxophlutist
answered 7 Months ago
98

Use parent as predefined reference: parent::run(). This will ensure you call parent method. The same way you could call first parent constructor first or after child one - parent::__construct().

Class Child extends Parent {
    public function __construct() {
      parent::__construct();
      // Access parent methods here?
      $some_arg = NULL; // init from constructor argument or somewhere else
      parent::run($some_arg); // explicitly call parent method
      // $this->run($some_arg); // implicitly will call parent if no child override
    }  

}

If you dont have an implementation in child you could call $this->run($args), where it will again call parent run method.

Wednesday, March 31, 2021
 
the_e
answered 9 Months ago
89

The correct way is to add a method DoSomeMagic() in the base class, with default implementation, or abstract. The derived class should than override it to do its magic.

Something like this maybe:

public class WireLessDevice
{ // base class
    protected virtual void ParseMessage()
    {
        // Do common stuff in here
    }
}

public class WiFi : WireLessDevice
{ // derived class
    override void ParseMessage()
    {
        base.ParseMessage();//Call this if you need some operations from base impl.
        DoGPSStuff();
    }
    private void DoGPSStuff()
    {
        //some gps stuff
    }
    GPSLoc Loc;
}
Sunday, August 15, 2021
 
mnagel
answered 4 Months ago
69

These two lines:

x=Heap()
x.__init__([42,32,54,1,3])

should be one:

x=Heap([42,32,54,1,3])

Remember that Heap.__init__ is called implicitly when you do Heap(). Below is a demonstration:

>>> class Foo:
...     def __init__(self):
...         print("Foo.__init__ was called")
...
>>> Foo()
Foo.__init__ was called
<__main__.Foo object at 0x020AF030>
>>>
Sunday, August 22, 2021
 
Dance Party2
answered 4 Months ago
21

Both getX and getY are methods in your code, not attributes. So you will need to call them using getX() and getY().

So ma=(p2.getY-p1.getY)/(p2.getX-p1.getX) becomes:

ma = (p2.getY()-p1.getY())/(p2.getX()-p1.getX())

And so on, the other code changes.

Otherwise, you can also define your methods as @property:

class Point:
    ...
    ...
    @property
    def getX(self):
        return self.x
    @property
    def getY(self):
        return self.y
    ...

And now you can access these as p1.getX and p2.getY and so on.

Note that the above @property decorator turns the method into a getter, which makes sense to use only with private variables (variables defined to start with _).

As such, since both x and y are normal attributes of your class, you can access them directly without using and property decorators or using getter methods, like p1.x and p2.y, as @Padraic points in his post.

Thursday, September 2, 2021
 
Valentin Radu
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share