KEMBAR78
Python - OOP Programming | PPTX
Python
Object Oriented Programming
Portland Data Science Group
Created by Andrew Ferlitsch
Community Outreach Officer
July, 2017
Class Definition
• Class defined with the keyword class.
class Node:
….
• Constructor defined with the special reserved name __init__.
class Node:
def __init__( self ):
…
…
Class keyword
Class Name
Keyword to define a method (function)
Reserved name for constructor
Required first parameter, self refers to this instantiated instance of the class.
Class Example
# Base Class definition for Node in Tree/Graph
class Node:
key = None # node data
# constructor: set the node data
def __init__( self, key ):
self.key = key
# Get or Set the node data
def Key( self, key = None ):
if None is key:
return self.key
self.key = key
Start of class definition
Initialization if member variable
Constructor with parameter
Use keyword self to refer to member variable
Define class method
Class Scope
• Parameter to Methods.
class Node:
def myFunc( self, flag )
if flag == True:
• Local to Method
class Node:
def myFunc( self ):
flag = True
• Class Member Variable
class Node:
flag = False
def myFunc( self ):
self.flag = True
• Global Scope
class Node:
def myFunc( self ):
global flag
Parameter ‘flag’
Referenced without qualifier
Referenced without qualifier and not defined as parameter.
Referenced with qualifier self, refers to class variable
When declared with global keyword, all references in method
Will refer to the global (not local) scope of variable.
Method Overloading
• There is NO direct support for method overloading in Python.
• Can be emulated (spoofed) by using Default parameters.
• Example: Setter and Getter
Next() # get the next element
Next(ele) # set the next element
• Without method overloading
# Set Next
def Next( self, next )
self.next = next
# Get Next
def GetNext(self)
return self.next
Must use different function name
Method Overloading using Default Parameter
• A default parameter to a function is where a default value is
specified in the function definition, when the function is called
without the parameter.
• Example: Setter and Getter
# Set of Get Next
def Next( self, next = None )
if next is None:
return self.next
self.next = next
Default Paramter
Get
Set
Operator Overloading
• Built-in operators (+, -, str(), int(), … ) can be overloaded for a
class object in Python.
• Each class has a default implementation for built-in operators.
the default implementations (magic methods) can be overridden to
implement operator overloading.
• Built-in operators are designated using the double dash __name__
convention. Below are some of these magic methods:
__str__ string conversion __add__ + operator
__int__ integer conversion __sub__ - operator
__eq__ == operator __mul__ * operator
__ne__ != operator __divmod__ / and % operator
__lt__ < operator __len__ len() operator
__le__ <= operator __getitem__ [] index operator
Operator Overloading
• Example:
class Node(object):
def __init__(self, name):
self.name = name
def __str__( self ):
return name
def__eq__( self, other ):
return (self.name == other.name)
node = Node( ‘foobar’)
print (str( node ) ) # will print foobar
Override string conversion
Override equal comparison
A full list of magic (special) methods that can be overwritten is available on the python.org website:
https://docs.python.org/3/reference/datamodel.html#special-method-names
Class Inheritance
• Defines a new class that extends a base (superclass).
• The new class (subclass) inherits the methods and member
variables of the base (superclass).
class BinaryTree( Node ):
• Invoking the base (superclass) constructor
Derived (subclass) name Base (superclass) that is inherited
class BinaryTree( Node ):
# Constructor: set the node data and left/right subtrees to null
def __init__( self, key ):
super().__init__( self, key )
Derived (subclass) constructor
Invoke base (superclass) constructor
Good Practice: it's generally considered good practice to have non-inheriting classes explicitly inherit from the "object" class.
Example: class Node: would become class Node(object):

Python - OOP Programming

  • 1.
    Python Object Oriented Programming PortlandData Science Group Created by Andrew Ferlitsch Community Outreach Officer July, 2017
  • 2.
    Class Definition • Classdefined with the keyword class. class Node: …. • Constructor defined with the special reserved name __init__. class Node: def __init__( self ): … … Class keyword Class Name Keyword to define a method (function) Reserved name for constructor Required first parameter, self refers to this instantiated instance of the class.
  • 3.
    Class Example # BaseClass definition for Node in Tree/Graph class Node: key = None # node data # constructor: set the node data def __init__( self, key ): self.key = key # Get or Set the node data def Key( self, key = None ): if None is key: return self.key self.key = key Start of class definition Initialization if member variable Constructor with parameter Use keyword self to refer to member variable Define class method
  • 4.
    Class Scope • Parameterto Methods. class Node: def myFunc( self, flag ) if flag == True: • Local to Method class Node: def myFunc( self ): flag = True • Class Member Variable class Node: flag = False def myFunc( self ): self.flag = True • Global Scope class Node: def myFunc( self ): global flag Parameter ‘flag’ Referenced without qualifier Referenced without qualifier and not defined as parameter. Referenced with qualifier self, refers to class variable When declared with global keyword, all references in method Will refer to the global (not local) scope of variable.
  • 5.
    Method Overloading • Thereis NO direct support for method overloading in Python. • Can be emulated (spoofed) by using Default parameters. • Example: Setter and Getter Next() # get the next element Next(ele) # set the next element • Without method overloading # Set Next def Next( self, next ) self.next = next # Get Next def GetNext(self) return self.next Must use different function name
  • 6.
    Method Overloading usingDefault Parameter • A default parameter to a function is where a default value is specified in the function definition, when the function is called without the parameter. • Example: Setter and Getter # Set of Get Next def Next( self, next = None ) if next is None: return self.next self.next = next Default Paramter Get Set
  • 7.
    Operator Overloading • Built-inoperators (+, -, str(), int(), … ) can be overloaded for a class object in Python. • Each class has a default implementation for built-in operators. the default implementations (magic methods) can be overridden to implement operator overloading. • Built-in operators are designated using the double dash __name__ convention. Below are some of these magic methods: __str__ string conversion __add__ + operator __int__ integer conversion __sub__ - operator __eq__ == operator __mul__ * operator __ne__ != operator __divmod__ / and % operator __lt__ < operator __len__ len() operator __le__ <= operator __getitem__ [] index operator
  • 8.
    Operator Overloading • Example: classNode(object): def __init__(self, name): self.name = name def __str__( self ): return name def__eq__( self, other ): return (self.name == other.name) node = Node( ‘foobar’) print (str( node ) ) # will print foobar Override string conversion Override equal comparison A full list of magic (special) methods that can be overwritten is available on the python.org website: https://docs.python.org/3/reference/datamodel.html#special-method-names
  • 9.
    Class Inheritance • Definesa new class that extends a base (superclass). • The new class (subclass) inherits the methods and member variables of the base (superclass). class BinaryTree( Node ): • Invoking the base (superclass) constructor Derived (subclass) name Base (superclass) that is inherited class BinaryTree( Node ): # Constructor: set the node data and left/right subtrees to null def __init__( self, key ): super().__init__( self, key ) Derived (subclass) constructor Invoke base (superclass) constructor Good Practice: it's generally considered good practice to have non-inheriting classes explicitly inherit from the "object" class. Example: class Node: would become class Node(object):