perl objects and python class part 1.

Contrast with perl.

Basic 1

perl


package Foo;
sub new  { bless { a => pop }, shift }
sub echo { print shift->{a}, "\n" }
1;

Foo->new("foo")->echo ;

python


class Foo(object):
    def __init__(self,a):
        self.a = a
    def echo(self):
        print(self.a)

Foo("foo").echo()

Basic 2

perl


package Foo;
sub new { bless {}, shift }
sub add { ++ shift->{c} }
1;

my $c = Foo->new ;
print $c->add . "\n" for 0 .. 9 ;

python


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

o = Foo()
for i in range(10):
    print (o.add())

Hash (dictionary) arguments

perl


package Foo;
sub new   { bless { c => 10, @_[1..$#_]}, shift }
1;

use Data::Dumper ;
my $obj = Foo->new( d => 10) ;
print Dumper $obj ;

python


class Foo(object):
    def __init__(self, **args):
        self.__dict__ = { 'c':10 }
        self.__dict__.update( args )

o = Foo(d=10)
print (vars(o))

Singleton

perl


package Foo;

my $s ;
sub new { $s ||= bless {}, shift ; }
sub add { ++ shift->{c} }

1;

my $o = Foo->new ;
my $p = Foo->new ;
print  $o . "\n";
print  $p . "\n";
print $o->add . "\n";
print $p->add . "\n";

python


class Foo(object):
    _instance = None
    def  __new__(cls):
        if cls._instance is None:
            cls._instance = object.__new__(cls)
        return cls._instance
    def __init__(self):
        self._x = 0
    def add(self):
        self._x = self._x + 1
        return self._x

o = Foo()
p = Foo()
print (id(o))
print (id(p))
print (o.add())
print (p.add())

Callback

Will be replaced with a slightly serious example

perl


package Foo;
sub new     { bless pop, shift ; } ;
sub execute { shift->(pop); }
1;

my $obj = Foo->new( sub { print $_[0] ; } ) ;
$obj->execute( 'foo' ) ;

python


#from __future__ import print_function
class Foo(object):
    def __init__(self, f):
        self._f = f
    def execute(self):
        self._f()

o = Foo(lambda : print ('foo'))
o.execute()

--Uncomment the comment if you want to use it in 2 series

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
[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
[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
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
Python exception class list
Sample of getting module name and class name in Python
[Python] How to play with class variables with decorator and metaclass