perl objects and python class part 2.

Inheritance

An example of defining a simple counter (ʻadd) and inheriting another counter (ʻaddc)

f.pl


package Foo;
sub new { bless {x => 0}, shift }
sub add { ++ shift->{x} }
1;

package Bar;
use base qw(Foo) ;
sub new  { my $r = Foo->new ; $r->{y} = 0 ; bless $r, shift }
sub addc { ++ shift->{y} }
1;

use Data::Dumper ;
my $o = Foo->new ;
printf "Foo add:    %s\n", $o->add for 0 .. 1 ;
printf Dumper $o ;

my $p = Bar->new ;
printf "Bar add:    %s\n", $p->add ;
printf "Bar addc:   %s\n", $p->addc for 0 .. 1 ;
printf Dumper $p ;

python


$ perl f.pl
Foo add:    1
Foo add:    2
$VAR1 = bless( {
                 'x' => 2
               }, 'Foo' );
Bar add:    1
Bar addc:   1
Bar addc:   2
$VAR1 = bless( {
                 'y' => 2,
                 'x' => 1
               }, 'Bar' );

f.py


class Foo(object):
    def __init__(self):
        self._x = 0
    def add(self):
        self._x = self._x + 1
        return self._x

class Bar(Foo):
    def __init__(self):
        super(Bar, self).__init__()
        self._y = 0
    def addc(self):
        self._y = self._y + 1
        return self._y


o = Foo()
for i in [0,1]:
    print ("Foo add:    {0}".format(o.add()))
print (vars(o))

p = Bar()
print ("Bar add:    {0}".format(p.add()))
for i in [0,1]:
    print ("Bar addc:   {0}".format(p.addc()))
print (vars(p))

python


$ python f.py
Foo add:    1
Foo add:    2
{'_x': 2}
Bar add:    1
Bar addc:   1
Bar addc:   2
{'_y': 2, '_x': 1}

Inheritance-Call the inheritance source method with an alias

If you want to give a method with the same name as the method of the inheritance source (parent, base class), but you also want to access the inheritance source.

An example of using the inheriting counter ʻadd as ʻaddp and redefining ʻadd` in the inherited one.

perl


package Foo;
sub new{ bless {x => 0}, shift }
sub add{ ++ shift->{x} }
1;

package Bar;
use base qw(Foo) ;
sub new  { my $r = Foo->new ; $r->{y} = 0 ; bless $r, shift }
sub add  { ++ shift->{y} }
sub addp { shift->SUPER::add }
1;

use Data::Dumper ;
my $o = Foo->new ;
printf "Foo add:    %s\n", $o->add for 0 .. 1 ;
printf Dumper $o ;

my $p = Bar->new ;
printf "Bar addp:   %s\n", $p->addp ;
printf "Bar add:    %s\n", $p->add for 0 .. 1 ;
printf Dumper $p ;

python


class Foo(object):
    def __init__(self):
        self._x = 0
    def add(self):
        self._x = self._x + 1
        return self._x

class Bar(Foo):
    def __init__(self):
        super(Bar, self).__init__()
        self._y = 0
    def add(self):
        self._y = self._y + 1
        return self._y
    def addp(self):
        return super(Bar, self).add()

o = Foo()
for i in [0,1]:
    print ("Foo add:    {0}".format(o.add()))
print (vars(o))

p = Bar()
print ("Bar addp:   {0}".format(p.addp()))
for i in [0,1]:
    print ("Bar add:    {0}".format(p.add()))
print (vars(p))

Recommended Posts

perl objects and python class part 2.
perl objects and python class part 1.
Python: Class and instance variables
About python objects and classes
About Python variables and objects
Python class variables and instance variables
Python class definitions and instance handling
Assignments and changes in Python objects
Examine the object's class in python
AM modulation and demodulation in Python Part 2
FM modulation and demodulation with Python Part 3
Python basic operation 3rd: Object-oriented and class
[Python] Difference between class method and static method
FM modulation and demodulation with Python Part 2
Talking about Python class attributes and metaclasses
QGIS + Python Part 2
[Python] class, instance
"Kanrika" python class
About python, class
QGIS + Python Part 1
Python: Scraping Part 1
Python class, instance
#Python basics (class)
Python3 Beginning Part 1
Python: Scraping Part 2
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
[python] Difference between variables and self. Variables in class
I wrote a class in Python3 and Java
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
Trouble with Python pseudo-private variables and class inheritance
[Python] Class type and usage of datetime module
[python] Compress and decompress
About Class and Instance
python syslog wrapper class
Python class (Python learning memo ⑦)
Python and numpy tips
The simplest Python memo in Japan (classes and objects)
[Python] pip and wheel
case class in python
[Python] Convert general-purpose container and class to each other
Batch design and python
Python iterators and generators
[Python] Class inheritance (super)
Python self-made class sort
[python] class basic methods
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
[Python] Class inheritance, override
python input and output
Python and Ruby split
Python basic memorandum part 2
python subprocess wrapper class
Python basic memo --Part 2
Installing Python 3 on Mac and checking basic operation Part 1
Class methods and static methods
Python3, venv and Ansible
Python asyncio and ContextVar
Python basic memo --Part 1
YOLO Python wrapper class
Class notation in Python