• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Python & Perl: Lecture 14
 

Python & Perl: Lecture 14

on

  • 808 views

 

Statistics

Views

Total Views
808
Views on SlideShare
500
Embed Views
308

Actions

Likes
0
Downloads
0
Comments
0

23 Embeds 308

http://vkedco.blogspot.com 222
http://www.vkedco.blogspot.com 36
http://vkedco.blogspot.in 14
http://vkedco.blogspot.co.il 6
http://vkedco.blogspot.nl 3
http://vkedco.blogspot.hu 2
http://vkedco.blogspot.fr 2
http://vkedco.blogspot.de 2
http://vkedco.blogspot.ca 2
http://vkedco.blogspot.pt 2
http://vkedco.blogspot.kr 2
http://vkedco.blogspot.mx 2
http://vkedco.blogspot.co.uk 2
http://vkedco.blogspot.jp 2
http://www.vkedco.blogspot.in 1
http://vkedco.blogspot.com.au 1
http://vkedco.blogspot.ru 1
http://vkedco.blogspot.co.nz 1
http://vkedco.blogspot.com.es 1
http://vkedco.blogspot.it 1
http://vkedco.blogspot.ro 1
http://vkedco.blogspot.be 1
http://vkedco.blogspot.sg 1
More...

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Python & Perl: Lecture 14 Python & Perl: Lecture 14 Presentation Transcript

    • Python & Perl Lecture 14 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
    • Outline ● Polymorphism & Duck Typing ● Encapsulation ● Inheritance ● Multiple Inheritance Pitfallswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Polymorphismwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Polymorphism ● Polymorphism is a noun derived from two Greek words poly (many) and morph (form) ● Wiktionary (http://en.wiktionary.org) defines polymorphism as the ability to assume multiple forms or shapes ● In OOP, polymorphism refers to the use of the same operation on objects of different classeswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example 1 >>> lst = [1, 2, 3] ## lst is a list >>> tup = (1, 2, 3) ## tup is a tuple >>> str = 123 ## str is a string >>> lst.count(1) ## count is polymorphic, applied to a list 1 >>> tup.count(1) ## count is polymorphic, applied to a tuple 1 >>> str.count(1) ## count is polymorphic, applied to a string 1www.youtube.com/vkedco www.vkedco.blogspot.com
    • Example 2 >>> lst = [1, 2, 3] ## lst is a list >>> tup = (1, 2, 3) ## tup is a tuple >>> str = 123 ## str is a string >>> len(lst) ## len is polymorphic 3 >>> len(tup) ## len is polymorphic 3 >>> len(str) ## len is polymorphic 3www.youtube.com/vkedco www.vkedco.blogspot.com
    • Rileys Duck Test ● In Python, polymorphism is synonymous with duck typing ● Duck typing allegedly takes its name from the duck test attributed to James Whitcomb Riley, an American writer and poet: “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”www.youtube.com/vkedco www.vkedco.blogspot.com
    • Rileys Duck Test ● The basic principle of duck typing can be expressed as follows: it is not the type of the object that matters but the operations that the object supports ● In non-duck-typed languages: – the programmer defines the functions walk and quack as walk(Duck x) and quack(Duck x) – If x is not of type Duck, a compile time error is signaled ● In duck-typed languages: – the programmer defines the functions walk and quack as walk(x) and quack(x) – If x is not of type Duck, a run time error is signaledwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example from en.wikipedia.org/wiki/Duck_typing class Duck: def quack(self): print Quack! def feathers(self): print The duck has white and gray feathers. class Person: def quack(self): print The person imitates a duck. def feathers(self): print The person takes a feather from the ground and shows it def name(self): print self.namewww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example from en.wikipedia.org/wiki/Duck_typing ## in_the_forest is a duck-typing function def in_the_forest(x): ## whatever the type of x is, just call quack ## and feathers on it x.quack() x.feathers()www.youtube.com/vkedco www.vkedco.blogspot.com
    • Example from en.wikipedia.org/wiki/Duck_typing ## game is played correctly ## run-time error occurs def game_01(): def game_02(): print Game 01 print Game 02 x1 = Duck() x1 = Duck() x2 = Person() x2 = [1, 2, 3] x2.name = John in_the_forest(x1) in_the_forest(x1) in_the_forest(x2) in_the_forest(x2)www.youtube.com/vkedco www.vkedco.blogspot.com
    • Example from en.wikipedia.org/wiki/Duck_typing >>> game_01() Game 01 Quack! The duck has white and gray feathers. The person imitates a duck. The person takes a feather from the ground and shows itwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example from en.wikipedia.org/wiki/Duck_typing >>> game_02() Game 02 Quack! The duck has white and gray feathers. AttributeError: list object has no attribute quackwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Critique of Duck Typing ● Duck typing increases the cognitive load on the programmer because the programmer cannot infer types from local code segments and therefore must always be aware of the big picture ● Duck typing makes project planning more difficult because in many cases only project managers need to know the big picture ● Duck typing increases software testingwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Encapsulationwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Encapsulation ● Encapsulation is the principle of hiding unnecessary information from the world ● A class defines the data that its objects need ● Users of objects may not want to know most of the data ● Example: A snowflake class defines all the necessary components for the snowflake to be drawn but the end users are only interested in drawing snowflakeswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Polymorphism vs. Encapsulation ● Both polymorphism and encapsulation are data abstraction principles ● Polymorphism allows the programmer to call the methods of an object without knowing the objects type ● Encapsulation allows the programmer to manipulate the objects of a class without knowing how the objects are constructedwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Encapsulation & Privacy ● Python classes do not support privacy in the C++ or Java sense of that term ● There is nothing in a Python class that can be completely hidden from the outside world ● To make a method or an attribute partially hidden, start it with two underscoreswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Encapsulation & Privacy class C: x=0 __y = 1 ## y has two underscores in front of it def __g(self): ## g has two underscores in front of it print semi private def f(self): self.__g()www.youtube.com/vkedco www.vkedco.blogspot.com
    • Encapsulation & Privacy >>> c = C() >>> c.f() semi private >>> c.x 0 >>> c.__y ## error >>> c.__g() ## errorwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Encapsulation & Privacy ● Python converts all names that begin with double underscores into the same names that are prefixed with a single underscore and the class name. So they are still accessible! >>> c = C() >>> C._C__y ## accessing __y 1 >>> C._C__g(c) ## accessing __g semi privatewww.youtube.com/vkedco www.vkedco.blogspot.com
    • Class Scope ● Variables defined in the class scope are accessible to all instances of the class class D: count = 0 def incr_count(self): D.count += 1 def get_count(self): return D.countwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Class Scope >>> d1 = D() >>> d1.incr_count() >>> D.count 1 >>> d2 = D() >>> d2.incr_count() >>> D.count 2www.youtube.com/vkedco www.vkedco.blogspot.com
    • Inheritancewww.youtube.com/vkedco www.vkedco.blogspot.com
    • Inheritance ● Inheritance is an OOP principle that supports code reuse and abstraction ● If a class C defines a set of attributes (data and methods), the programmer can derive a subclass of C without having to reimplement Cs attributes ● In OOP, subclasses are said to inherit attributes of superclasseswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example class A: def f(self): print As f. def g(self): print As g. ## B inherits f from A and overrides g class B(A): def g(self): print Bs g.www.youtube.com/vkedco www.vkedco.blogspot.com
    • Example >>> a = A() >>> b = B() >>> a.f() As f. >>> a.g() As g. >>> b.f() As f. >>> b.g() Bs g.www.youtube.com/vkedco www.vkedco.blogspot.com
    • Checking Inheritance >>> B.__bases__ (<class __main__.A>,) >>> issubclass(A, B) False >>> issubclass(B, A) Truewww.youtube.com/vkedco www.vkedco.blogspot.com
    • Checking Inheritance >>> a = A() >>> isinstance(a, A) True >>> b = B() >>> isinstance(b, B) True >>> isinstance(b, A) Truewww.youtube.com/vkedco www.vkedco.blogspot.com
    • Multiple Superclasses ● A class in Python can have multiple superclasses: this is called multiple inheritance ● Caveat: if several superclasses implement the same method, the method resolution order is required ● The method resolution order is a graph search algorithm ● General advice: unless you really need multiple inheritance, you should avoid itwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example Calculator Talker calculate talk TalkingCalculator calculate talkwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Inheritance __metaclass__ = type class Calculator: def calculate(self, expression): self.value = eval(expression) class Talker: def talk(self): print My value is, self.value class TalkingCalculator(Calculator, Talker): passwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Multiple Inheritance >>> tc = TalkingCalculator() ## calculate is inherited from Calculator >>> tc.calculate(1 + 2) ## talk is inherited from Talker >>> tc.talk() My value is 3www.youtube.com/vkedco www.vkedco.blogspot.com
    • Multiple Inheritance Pitfalls __metaclass__ = type class A: def f(self): print "As f" class B: def f(self): print "Bs f" ## AB inherits first from A and then from B class AB(A, B): pass ## BA inherits first from B and then from A class BA(B, A): passwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Multiple Inheritance Pitfalls ## ab first inherits from A then from B >>> ab = AB() ## ba first inherits from B then from A >>> ba = BA() ## f is inherited from A >>> ab.f() As f ## f is inherited from B >>> ba.f() Bs fwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Reading & References ● www.python.org ● http://en.wikipedia.org/wiki/Duck_typing ● http://en.wikipedia.org/wiki/James_Whitcomb_Riley ● Ch 07 M. L. Hetland. Beginning Python From Novice to Pro- nd fessional, 2 Ed., APRESSwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Feedback Bugs, comments to vladimir dot kulyukin at usu dot eduwww.youtube.com/vkedco www.vkedco.blogspot.com