Security Engineer/Consultant at AllMed Healthcare Management
17 de Oct de 2017•0 gostou•1,012 visualizações
1 de 9
Python - OOP Programming
17 de Oct de 2017•0 gostou•1,012 visualizações
Baixar para ler offline
Denunciar
Tecnologia
Abstract: This PDSG workshop covers the basics of OOP programming in Python. Concepts covered are class, object, scope, method overloading and inheritance.
Level: Fundamental
Requirements: One should have some knowledge of programming.
2. 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.
3. 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
4. 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.
5. 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
6. 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
7. 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
8. 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
9. 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):