Your SlideShare is downloading. ×
Python & Perl: Perl Object Construction & Destruction, Python Object Construction & Destruction, Python Magic Methods, Python Superclass Constructors
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Python & Perl: Perl Object Construction & Destruction, Python Object Construction & Destruction, Python Magic Methods, Python Superclass Constructors

3,842
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,842
On Slideshare
0
From Embeds
0
Number of Embeds
29
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Python & Perl Perl Object Construction & Destruction, Python Object Construction & Destruction, Python Magic Methods, Python Superclass Constructors Vladimir Kulyukinwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Quote I dont predict the demise of object-oriented programming, by the way. Though I dont think it has much to offer good programmers, except in certain specialized domains, it is irresistible to large organizations. Object-oriented programming offers a sustainable way to write spaghetti code. It lets you accrete programs as a series of patches. Large organizations always tend to develop software this way, and I expect this to be as true in a hundred years as it is today. Paul Graham, The Hundred Year Languagewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 3. Outline ● Perl Object Construction & Destruction ● Python Object Construction & Destruction – Magic Methods – Superclass Constructors ● Python Protocols ● Python Sequence Protocolwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 4. Perl Object Construction & Destructionwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 5. Perl ref() Function ● ref(expr) returns a non-empty string if expr is a reference and an empty string otherwise my %h = {}; my $href = %h; print ref($href), “n”; ## prints HASH my @a = (); my $aref = @a; print ref($aref), “n”; ## prints ARRAYwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 6. Object Construction Methods ● Suppose Type is a class type defined in Type.pm ● Method 01: Type->new() ● Method 02: new Type() or new Type ● Method 03: Suppose $obj_ref is a reference to a Type object, then $obj_ref->new()www.youtube.com/vkedco www.vkedco.blogspot.com
  • 7. Exercise Design and implement a Perl Date class. The class has three data attributes: day, month, and year. The class should define getters and setters for each data attribute. source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 8. Date.pm: Standard Constructor sub new { my $type = shift(); my $class = ref($type) || $type; ## $class is a type str my $date_hash = { mMonth => 1, mDate => 1, mYear => 0 }; bless( $date_hash, $class ); return $date_hash; }www.youtube.com/vkedco www.vkedco.blogspot.com
  • 9. Date.pm: Custom Constructor sub make { my $type = shift(); my $class = ref($type) || $type; my $date_hash = { mMonth => 1, mDate => 1, mYear => 0 }; bless( $date_hash, $class ); return $date_hash; }www.youtube.com/vkedco www.vkedco.blogspot.com
  • 10. Date.pm: Methods sub year { my $this = shift(); ## if there is a second argument, then set the value of ## the key mYear If ( @_ ) { $this->{ mYear } = shift(); } return $this->{ mYear }; } sub month { my $this = shift(); if ( @_ ) { $this->{ mMonth } = shift(); } return $this->{ mMonth }; }www.youtube.com/vkedco www.vkedco.blogspot.com
  • 11. Date.pm: Methods sub day { my $this = shift(); if ( @_ ) { $this->{ mDay } = shift(); } return $this->{ mDay }; } sub printMDY { my $this = shift(); print $this->month(), /, $this->day(), /, $this->year(); }www.youtube.com/vkedco www.vkedco.blogspot.com
  • 12. Date.pm: Destructor ## This is one of the so-called implicit functions. ## Check online doc for other implicit functions: BEGIN, END, AUTOLOAD. ## Unlike the name of the constructor, i.e., new, DESTROY cannot be given a ## different name. sub DESTROY { my $this = shift(); print Destroying Date ; $this->printMDY(); print "n"; $this->{ mMonth } = undef; $this->{ mYear } = undef; $this->{ mDay } = undef; }www.youtube.com/vkedco www.vkedco.blogspot.com
  • 13. Python Magic Methodswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 14. Magic Methods ● Magic methods in Python start and end with double underscores __ ● Magic methods are called by Python under specific circumstances: – __init__()- called when objects are constructed – __eq__() - called when objects are comparedwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 15. Magic Method __init__() ● When a class defines __init__(), it is called when instances of that class are constructed ## __init__ with no ## __init__ with one argument ## arguments class A2: class A1: def __init__(self, x=10): def __init__(self): self.x = x self.x = 10www.youtube.com/vkedco www.vkedco.blogspot.com
  • 16. Magic Method __del__() ● When a class defines __del__(), it is called when instances of that class are destroyed by the garbage collector SOURCE OUTPUT class A3: >>> a = A3(x=200) def __init__(self, x=10): >>> del a self.my_list = [i for i in xrange(x)] Destroying an A3 object def __del__(self): print Destroying an A3 object del self.my_listwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 17. Overriding __init__() ● Magic method __init__() can be overriden ● When a class A overrides __init__() and its superclasses contain attributes that A objects use, As __init__() must call explicitly its superclass constructors ● If As __init__() does not call superclass constructors, run-time attribute access errors are likelywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 18. Example SOURCE OUTPUT __metaclass__ = type >>> p1 = TwoDPoint(10, 10) import math >>> p2 = ThreeDPoint(30) class TwoDPoint: >>> p2.x def __init__(this, x=0, y=0): AttributeError: ThreeDPoint object has no attribute x this.x = x >>> p2.y this.y = y AttributeError: ThreeDPoint object def distance_from_origin(this): has no attribute y return math.hypot(this.x, this.y) class ThreeDPoint(TwoDPoint): def __init__(this, z=0): this.z = z source is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 19. Fixing Attribute Construction Problem ● ThreeDPoint inherits from TwoDPoint ● However, as the examples output shows, ThreeDPoint objects do not have the attributes x and y ● The problem can be fixed by having ThreeDPoints constructor explicitly call the constructor of its superclass ● There are two ways of calling superclass constructors: – Unbound superclass constructor – The function super()www.youtube.com/vkedco www.vkedco.blogspot.com
  • 20. Calling Superclass Constructorswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 21. Unbound Superclass Constructor SuperClass.__init__(self, arguments) – Superclass is the superclass of selfs class – arguments are the arguments to SuperClasss constructorwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 22. Example SOURCE OUTPUT __metaclass__ = type >>> p = ThreeDPoint(10, 20, 30) import math >>> p.x class TwoDPoint: 10 def __init__(this, x=0, y=0): >>> p.y this.x = x 20 this.y = y >>> p.z def distance_from_origin(this): 30 return math.hypot(this.x, this.y) class ThreeDPoint(TwoDPoint): def __init__(this, x=0, y=0, z=0): TwoDPoint.__init__(this, x, y) this.z = zwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 23. Dealing with Multiple Inheritance ● An appropriate unbound superclass constructor must be called for every inherited attribute ● If class A inherits attribute x from class B and attribute y from class C, As constructor must call both constructors to ensure that both attributes are constructedwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 24. Example SOURCE OUTPUT class A: >>> c = C(10, 20, 30) def __init__(this, x=0): >>> c.x this.x = x 10 class B: >>> c.y def __init__(this, y=0): 20 this.y = y >>> c.z class C(A, B): 30 def __init__(this, x=0, y=0, z=0): A.__init__(this, x) B.__init__(this, y) this.z = zwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 25. Calling Function super() super(Class, self).__init__(arguments) – Class is the class of self object – super(Class, self) returns the object of Classs superclass – arguments are the arguments to the __init__ method of Classs superclasswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 26. Example SOURCE OUTPUT class TwoDPoint: >>> p = ThreeDPoint(10, 20, 30) def __init__(this, x=0, y=0): >>> p.x this.x = x 10 this.y = y >>> p.y def dist_from_origin(this): 20 math.hypot(this.x, this.y) >>> p.z 30 class ThreeDPoint(TwoDPoint): def __init__(this, x=0, y=0, z=0): super(ThreeDPoint, this).__init__(x, y) this.z = zwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 27. Reading & References ● www.python.org ● http://en.wikipedia.org/wiki/Duck_typing ● http://www.pythonware.comwww.youtube.com/vkedco www.vkedco.blogspot.com