Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Sprites A sprite is a 2D image or animation that is integrated into a larger scene. Originally, sprites were created by special hardware that would super-impose.
Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein Classes and Objects Object Oriented Programming.
Python Objects and Classes
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Classes 2 COMPSCI 105 SS 2015 Principles of Computer Science.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein Classes and Objects Object Oriented Programming.
Road Map Introduction to object oriented programming. Classes
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
 2002 Prentice Hall. All rights reserved. 1 Chapter 8 – Customizing Classes Outline 8.1 Introduction 8.2 Customizing String Representation: Method __str__.
1 Python Chapter 3 Reading strings and printing. © Samuel Marateck.
Introduction to Python
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Python classes: new and old. New and classic classes  With Python 2.2, classes and instances come in two flavors: old and new  New classes cleaned up.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Lab 8 Shell Script Reference:
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
Floating point numbers in Python Floats in Python are platform dependent, but usually equivalent to an IEEE bit C “double” However, because the significand.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Invitation to Computer Science 5th Edition
Classes 3 COMPSCI 105 S Principles of Computer Science.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Introduction to Python III CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
CS61A Lecture 15 Object-Oriented Programming, Mutable Data Structures Jom Magrotker UC Berkeley EECS July 12, 2012.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Applications Development
P YTHON ’ S C LASSES Ian Wynyard. I NTRODUCTION TO C LASSES A class is the scope in which code is executed A class contains objects and functions that.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
The CS 5 Herald Goodwill Gesture Goes Awry Claremont (AP) : Seven rooms were damaged in a Harvey Mudd College dormitory Tuesday evening after a misguided.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Object Oriented Programing (OOP)
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 12 Inheritance and Class Design 1.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Python Object Model Sambasiva Suda PyCon India. Agenda Python Classic Objects Different types of Objects Relationships among objects Q & A.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Q and A for Sections 6.2, 6.3 Victor Norman CS106.
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Containers and Lists CIS 40 – Introduction to Programming in Python
Software Development Java Classes and Methods
CS-104 Final Exam Review Victor Norman.
Copyright (c) 2017 by Dr. E. Horvath
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Creating and Deleting Instances Access to Attributes and Methods
CHAPTER FIVE Classes.
Object-oriented programming
Object Oriented Programming (OOP) LAB # 8
functions: argument, return value
Python classes: new and old
The Selection Structure
Python classes: new and old
Introduction to Object-Oriented Programming (OOP)
Introduction to Object-Oriented Programming (OOP) II
Presentation transcript:

Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1

Classes and Modules Python fully supports procedural and object-oriented programming The syntax for creating a class is simple: class className(base_classes): suite Python fully supports procedural and object-oriented programming The syntax for creating a class is simple: class className(base_classes): suite 2

Creating Instances Python has the __new__() special method which is called to construct an object, the __init__() special method which is called to initialize a newly constructed object. When an object is about to be garbage-collected its __del__() special method is called, with self as its only argument. We will create one that stores a string (the name of a kind of chair) and a number (how many legs the chair has): class Chair(object): """This class represents chairs.""" def __init__(self, name, legs=4): self.name = name self.name = name self.legs = legs self.legs = legs Python has the __new__() special method which is called to construct an object, the __init__() special method which is called to initialize a newly constructed object. When an object is about to be garbage-collected its __del__() special method is called, with self as its only argument. We will create one that stores a string (the name of a kind of chair) and a number (how many legs the chair has): class Chair(object): """This class represents chairs.""" def __init__(self, name, legs=4): self.name = name self.name = name self.legs = legs self.legs = legs 3

Creating Instances To create an instance of a class, we use the following syntax: instance = className(arguments) for example: chair1 = Chair("Barcelona") chair2 = Chair("Bar Stool", 1) Since the attributes are public, they can be read or assigned to using the dot (.) operator; for example: print chair2.name will print “Bar Stool”, and chair1.legs = 2 will change chair1’s legs attribute’s value from 4 to 2. To create an instance of a class, we use the following syntax: instance = className(arguments) for example: chair1 = Chair("Barcelona") chair2 = Chair("Bar Stool", 1) Since the attributes are public, they can be read or assigned to using the dot (.) operator; for example: print chair2.name will print “Bar Stool”, and chair1.legs = 2 will change chair1’s legs attribute’s value from 4 to 2. 4

Methods and Special Methods class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height def getWidth(self): return self.width def setWidth(self, width): self.width = width def getHeight(self): return self.height def setHeight(self, height): self.height = height def area(self): return self.getWidth() * self.getHeight() class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height def getWidth(self): return self.width def setWidth(self, width): self.width = width def getHeight(self): return self.height def setHeight(self, height): self.height = height def area(self): return self.getWidth() * self.getHeight() 5

Methods and Special Methods rect = Rectangle(50, 10) print rect.area() # Prints "500" rect.setWidth(20) rect = Rectangle(50, 10) print rect.area() # Prints "500" rect.setWidth(20) 6

Methods and Special Methods property() function class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height def _area(self): return self.width * self.height area = property(fget=_area) property() function class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height def _area(self): return self.width * self.height area = property(fget=_area) 7

Methods and Special Methods rect = Rectangle(5, 4) print rect.width, rect.height, rect.area # Prints (5, 4, 20) rect.width = 6 rect = Rectangle(5, 4) print rect.width, rect.height, rect.area # Prints (5, 4, 20) rect.width = 6 8

Methods and Special Methods def _width(self): return self.__width def _setWidth(self, width): # Perform some computation self.__width = width width = property(fget=_width, fset=_setWidth) def _width(self): return self.__width def _setWidth(self, width): # Perform some computation self.__width = width width = property(fget=_width, fset=_setWidth) 9

Methods and Special Methods class Rectangle(object): def __init__(self, width, height): self.__width = width self.__height = height def _area(self): return self.__width * self.__height area = property(fget=_area) def _height(self): return self.__height def _setHeight(self, height): self.__height = height height = property(fget=_height, fset=_setHeight) height = property(fget=_height, fset=_setHeight) class Rectangle(object): def __init__(self, width, height): self.__width = width self.__height = height def _area(self): return self.__width * self.__height area = property(fget=_area) def _height(self): return self.__height def _setHeight(self, height): self.__height = height height = property(fget=_height, fset=_setHeight) height = property(fget=_height, fset=_setHeight) 10

Methods and Special Methods def _width(self): def _width(self): return self.__width return self.__width def _setWidth(self, width): def _setWidth(self, width): self.__width = width self.__width = width width = property(fget=_width, fset=_setWidth) def __cmp__(self, other): return cmp(self.area, other.area) def __nonzero__(self): return self.__width or self.__height def __repr__(self): return "Rectangle(%d, %d)" % (self.__width, self.__height) def _width(self): def _width(self): return self.__width return self.__width def _setWidth(self, width): def _setWidth(self, width): self.__width = width self.__width = width width = property(fget=_width, fset=_setWidth) def __cmp__(self, other): return cmp(self.area, other.area) def __nonzero__(self): return self.__width or self.__height def __repr__(self): return "Rectangle(%d, %d)" % (self.__width, self.__height) 11

Methods and Special Methods print ("Starting..\n") rect1 = Rectangle(4,5) print ("Area is [",rect1.area,"]") print ("Set width to [ 7 ] and height to [ 2 ]") rect1.width = 7 rect1.height = 2 print ("Now area is [",rect1.area,"]") print ("\nFinishing..") print ("Starting..\n") rect1 = Rectangle(4,5) print ("Area is [",rect1.area,"]") print ("Set width to [ 7 ] and height to [ 2 ]") rect1.width = 7 rect1.height = 2 print ("Now area is [",rect1.area,"]") print ("\nFinishing..") 12

Methods and Special Methods Output:-Starting.. Area is [ 20 ] Set width to [ 7 ] and height to [ 2 ] Now area is [ 14 ] Finishing..Output:-Starting.. Area is [ 20 ] Set width to [ 7 ] and height to [ 2 ] Now area is [ 14 ] Finishing.. 13

Methods and Special Methods Method Syntax Description __init__(self, args) x = X() Initializes a newly created instance __call__(self, args) x() Makes instances callable, that is,turns them into functors. The args are optional. __cmp__(self, other)x == y x < y # etc Returns -1 if self < other, 0 if they are equal, and 1 otherwise. If __cmp__() is implemented, it will be used for any comparison operators that are not explicitly implemented. __eq__(self, other) x == y Returns True if x is equal to y __ne__(self, other)x != y Returns True if x is not equal to y __le__(self, other) x <= y Returns True if x is less than or equal to y Method Syntax Description __init__(self, args) x = X() Initializes a newly created instance __call__(self, args) x() Makes instances callable, that is,turns them into functors. The args are optional. __cmp__(self, other)x == y x < y # etc Returns -1 if self < other, 0 if they are equal, and 1 otherwise. If __cmp__() is implemented, it will be used for any comparison operators that are not explicitly implemented. __eq__(self, other) x == y Returns True if x is equal to y __ne__(self, other)x != y Returns True if x is not equal to y __le__(self, other) x <= y Returns True if x is less than or equal to y 14

Methods and Special Methods Method Syntax Description __lt__(self, other) x < y Returns True if x is less than y __ge__(self, other) x >= y Returns True if x is greater than or equal to y __gt__(self, other) x > y Returns True if x is greater than y __nonzero__(self) if x: passReturns True if x is nonzero passReturns True if x is nonzero __repr__(self) y = eval(`x`) Returns an eval()-able representation of x. Using backticks is the same as calling repr(). __str__(self) print x Returns a human-readable representation of x __unicode__(self) print x Returns a human-readable Unicode representation of x Method Syntax Description __lt__(self, other) x < y Returns True if x is less than y __ge__(self, other) x >= y Returns True if x is greater than or equal to y __gt__(self, other) x > y Returns True if x is greater than y __nonzero__(self) if x: passReturns True if x is nonzero passReturns True if x is nonzero __repr__(self) y = eval(`x`) Returns an eval()-able representation of x. Using backticks is the same as calling repr(). __str__(self) print x Returns a human-readable representation of x __unicode__(self) print x Returns a human-readable Unicode representation of x 15

Methods and Special Methods def __cmp__(self, other): return cmp(self.area(), other.area()) rectA = Rectangle(4, 4) rectB = Rectangle(8, 2) rectA == rectB # True because both have the same area rectA < rectB # False def __cmp__(self, other): if (self.width != other.width): return cmp(self.width, other.width) return cmp(self.height, other.height) def __cmp__(self, other): return cmp(self.area(), other.area()) rectA = Rectangle(4, 4) rectB = Rectangle(8, 2) rectA == rectB # True because both have the same area rectA < rectB # False def __cmp__(self, other): if (self.width != other.width): return cmp(self.width, other.width) return cmp(self.height, other.height) 16

Methods and Special Methods def __nonzero__(self): return self.width or self.height) def __repr__(self): return "Rectangle(%d, %d)" % (self.width, self.height) def __nonzero__(self): return self.width or self.height) def __repr__(self): return "Rectangle(%d, %d)" % (self.width, self.height) 17

Methods and Special Methods Method Syntax MethodSyntax __float__(self)float(x) __int__(self)int(x) __abs__(self) abs(x) __neg__(self) -x __add__(self, other) x + y__sub__(self, other) x - y __iadd__(self, other) x +=y __isub__(self, other) x -= y __radd__(self, other) y + x__rsub__(self, other) y - x __mul__(self, other) x * y__mod__(self, other)x % y __imul__(self, other) x *= y__imod__(self, other)x %= y __rmul__(self, other) y * x __rmod__(self, other) y % x __floordiv__(self, other) x // y__truediv__(self, other) x / y __ifloordiv__(self, other)x //= y __itruediv__(self, other)x /= y __rfloordiv__(self, other) y // x __rtruediv__(self, other) y / x Method Syntax MethodSyntax __float__(self)float(x) __int__(self)int(x) __abs__(self) abs(x) __neg__(self) -x __add__(self, other) x + y__sub__(self, other) x - y __iadd__(self, other) x +=y __isub__(self, other) x -= y __radd__(self, other) y + x__rsub__(self, other) y - x __mul__(self, other) x * y__mod__(self, other)x % y __imul__(self, other) x *= y__imod__(self, other)x %= y __rmul__(self, other) y * x __rmod__(self, other) y % x __floordiv__(self, other) x // y__truediv__(self, other) x / y __ifloordiv__(self, other)x //= y __itruediv__(self, other)x /= y __rfloordiv__(self, other) y // x __rtruediv__(self, other) y / x 18

Static Data, and Static Methods and Decorators class Balloon(object): unique_colors = set() def __init__(self, color): self.color = color def uniqueColorCount(): return def uniqueColors(): return Balloon.unique_colors.copy() class Balloon(object): unique_colors = set() def __init__(self, color): self.color = color def uniqueColorCount(): return def uniqueColors(): return Balloon.unique_colors.copy() 19

Static Data, and Static Methods and Decorators class Example: staticVariable = 5 staticVariable = 5print("starting\n") # Access through class print (Example.staticVariable) # prints 5 # Access through instance instance = Example() print (instance.staticVariable) # still 5 class Example: staticVariable = 5 staticVariable = 5print("starting\n") # Access through class print (Example.staticVariable) # prints 5 # Access through instance instance = Example() print (instance.staticVariable) # still 5 20

Static Data, and Static Methods and Decorators # Change within instance instance.staticVariable = 6 print (instance.staticVariable) # 6 print (Example.staticVariable) # 5 # Change through class Example.staticVariable = 7 print (instance.staticVariable) # still 6 print (Example.staticVariable) # now 7 print("\nfinishing") # Change within instance instance.staticVariable = 6 print (instance.staticVariable) # 6 print (Example.staticVariable) # 5 # Change through class Example.staticVariable = 7 print (instance.staticVariable) # still 6 print (Example.staticVariable) # now 7 print("\nfinishing") 21

Static Data, and Static Methods and Decorators class Example(object): name = "Example" name def static(): def static(): print ("%s static() called" % Example.name) print ("%s static() called" % Example.name) class Offspring1(Example): name = "Offspring1" name = "Offspring1" class Example(object): name = "Example" name def static(): def static(): print ("%s static() called" % Example.name) print ("%s static() called" % Example.name) class Offspring1(Example): name = "Offspring1" name = "Offspring1" 22

Static Data, and Static Methods and Decorators class Offspring2(Example): name = "Offspring2" name def static(): def static(): print ("%s static() called" % Offspring2.name) print ("%s static() called" % Offspring2.name)print("starting\n") Example.static() # prints Example Offspring1.static() # prints Example Offspring2.static() # prints Offspring2 print("\nfinishing“) class Offspring2(Example): name = "Offspring2" name def static(): def static(): print ("%s static() called" % Offspring2.name) print ("%s static() called" % Offspring2.name)print("starting\n") Example.static() # prints Example Offspring1.static() # prints Example Offspring2.static() # prints Offspring2 print("\nfinishing“) 23

Static Data, and Static Methods and Decorators class Example: name = "Example" name def static(cls): def static(cls): print ("%s static() called" % cls.name) print ("%s static() called" % cls.name) class Offspring1(Example): name = "Offspring1" name = "Offspring1" pass pass class Example: name = "Example" name def static(cls): def static(cls): print ("%s static() called" % cls.name) print ("%s static() called" % cls.name) class Offspring1(Example): name = "Offspring1" name = "Offspring1" pass pass 24

Static Data, and Static Methods and Decorators class Offspring2(Example): name = "Offspring2" name def static(cls): def static(cls): print ("%s static() called" % cls.name) print ("%s static() called" % cls.name)print("starting\n") Example.static() # prints Example Offspring1.static() # prints Offspring1 Offspring2.static() # prints Offspring2 print("\nfinishing") class Offspring2(Example): name = "Offspring2" name def static(cls): def static(cls): print ("%s static() called" % cls.name) print ("%s static() called" % cls.name)print("starting\n") Example.static() # prints Example Offspring1.static() # prints Offspring1 Offspring2.static() # prints Offspring2 print("\nfinishing") 25

Inheritance and Polymorphism class Item(object): def __init__(self, artist, title, year=None): def __init__(self, artist, title, year=None): self.__artist = artist self.__artist = artist self.__title = title self.__title = title self.__year = year self.__year = year def artist(self): def artist(self): return self.__artist return self.__artist def setArtist(self, artist): def setArtist(self, artist): self.__artist = artist self.__artist = artist class Item(object): def __init__(self, artist, title, year=None): def __init__(self, artist, title, year=None): self.__artist = artist self.__artist = artist self.__title = title self.__title = title self.__year = year self.__year = year def artist(self): def artist(self): return self.__artist return self.__artist def setArtist(self, artist): def setArtist(self, artist): self.__artist = artist self.__artist = artist 26

Inheritance and Polymorphism def title(self): def title(self): return self.__title return self.__title def setTitle(self, title): def setTitle(self, title): self.__title = title self.__title = title def year(self): def year(self): return self.__year return self.__year def setYear(self, year): def setYear(self, year): self.__year = year self.__year = year def __str__(self): def __str__(self): year = "" year = "" if self.__year is not None: if self.__year is not None: year = " in %d" % self.__year year = " in %d" % self.__year return "%s by %s%s" % (self.__title, self.__artist, year) return "%s by %s%s" % (self.__title, self.__artist, year) def title(self): def title(self): return self.__title return self.__title def setTitle(self, title): def setTitle(self, title): self.__title = title self.__title = title def year(self): def year(self): return self.__year return self.__year def setYear(self, year): def setYear(self, year): self.__year = year self.__year = year def __str__(self): def __str__(self): year = "" year = "" if self.__year is not None: if self.__year is not None: year = " in %d" % self.__year year = " in %d" % self.__year return "%s by %s%s" % (self.__title, self.__artist, year) return "%s by %s%s" % (self.__title, self.__artist, year) 27

Inheritance and Polymorphism class Painting(Item): def __init__(self, artist, title, year=None): def __init__(self, artist, title, year=None): super(Painting, self).__init__(artist, title, year) super(Painting, self).__init__(artist, title, year) # Item.__init__(self, artist, title, year) # Item.__init__(self, artist, title, year) class Sculpture(Item): def __init__(self, artist, title, year=None, material=None): def __init__(self, artist, title, year=None, material=None): super(Sculpture, self).__init__(artist, title, year) super(Sculpture, self).__init__(artist, title, year) self.__material = material self.__material = material class Painting(Item): def __init__(self, artist, title, year=None): def __init__(self, artist, title, year=None): super(Painting, self).__init__(artist, title, year) super(Painting, self).__init__(artist, title, year) # Item.__init__(self, artist, title, year) # Item.__init__(self, artist, title, year) class Sculpture(Item): def __init__(self, artist, title, year=None, material=None): def __init__(self, artist, title, year=None, material=None): super(Sculpture, self).__init__(artist, title, year) super(Sculpture, self).__init__(artist, title, year) self.__material = material self.__material = material 28

Inheritance and Polymorphism def material(self): def material(self): return self.__material return self.__material def setMaterial(self, material): def setMaterial(self, material): self.__material = material self.__material = material def __str__(self): def __str__(self): materialString = "" materialString = "" if self.__material is not None: if self.__material is not None: materialString = " (%s)" % self.__material materialString = " (%s)" % self.__material return "%s%s" % (super(Sculpture, self).__str__(), materialString) return "%s%s" % (super(Sculpture, self).__str__(), materialString) def material(self): def material(self): return self.__material return self.__material def setMaterial(self, material): def setMaterial(self, material): self.__material = material self.__material = material def __str__(self): def __str__(self): materialString = "" materialString = "" if self.__material is not None: if self.__material is not None: materialString = " (%s)" % self.__material materialString = " (%s)" % self.__material return "%s%s" % (super(Sculpture, self).__str__(), materialString) return "%s%s" % (super(Sculpture, self).__str__(), materialString) 29

Inheritance and Polymorphism a = Painting("Cecil Collins", "The Sleeping Fool", 1943) print a # Prints "The Sleeping Fool by Cecil Collins in 1943" b = Sculpture("Auguste Rodin", "The Secret", 1925, "bronze") print b # Prints "The Secret by Auguste Rodin in 1925 (bronze)" a = Painting("Cecil Collins", "The Sleeping Fool", 1943) print a # Prints "The Sleeping Fool by Cecil Collins in 1943" b = Sculpture("Auguste Rodin", "The Secret", 1925, "bronze") print b # Prints "The Secret by Auguste Rodin in 1925 (bronze)" 30

Inheritance and Polymorphism class Title(object): def __init__(self, title): self.__title = title def title(self): return self.__title class Title(object): def __init__(self, title): self.__title = title def title(self): return self.__title 31

Inheritance and Polymorphism items = [] items.append(Painting("Cecil Collins", "The Poet", 1941)) items.append(Sculpture("Auguste Rodin", "Naked Balzac", 1917,"plaster")) items.append(Title("Eternal Springtime")) for item in items: print item.title() The Poet by Cecil Collins in 1941 Naked Balzac by Auguste Rodin in 1917 (plaster) Eternal Springtime items = [] items.append(Painting("Cecil Collins", "The Poet", 1941)) items.append(Sculpture("Auguste Rodin", "Naked Balzac", 1917,"plaster")) items.append(Title("Eternal Springtime")) for item in items: print item.title() The Poet by Cecil Collins in 1941 Naked Balzac by Auguste Rodin in 1917 (plaster) Eternal Springtime 32

Inheritance and Polymorphism class Item(object): def __init__(self, artist, title, year=None): def __init__(self, artist, title, year=None): self.__artist = artist self.__artist = artist self.__title = title self.__title = title self.__year = year def title(self): return self.__title return self.__title def setTitle(self, title): self.__title = title class Item(object): def __init__(self, artist, title, year=None): def __init__(self, artist, title, year=None): self.__artist = artist self.__artist = artist self.__title = title self.__title = title self.__year = year def title(self): return self.__title return self.__title def setTitle(self, title): self.__title = title 33

Inheritance and Polymorphism items = [] for item in items: print item.title() try: for item in items: print item.title() except AttributeError: pass items = [] for item in items: print item.title() try: for item in items: print item.title() except AttributeError: pass 34

Inheritance and Polymorphism for item in items: if isinstance(item, Item): print item.title() for item in items: if hasattr(item, "title"): print item.title() for item in items: if hasattr(item, "title") and callable(item.title): print item.title() for item in items: if isinstance(item, Item): print item.title() for item in items: if hasattr(item, "title"): print item.title() for item in items: if hasattr(item, "title") and callable(item.title): print item.title() 35

Modules and Multifile Applications import Item import mylibrary.Item import mylibrary.Item as Item import Item import mylibrary.Item import mylibrary.Item as Item 36