Python Oop
Python Oop
with Python
Summary
This presentation assumes audience have the knowledge
of Object-Oriented A & D and emphasize on OOP
programming with python
Introduces Pythons special methods to realize class
definition, inheritance, multiple inheritance, accessibility,
polymorphism, encapsulation.
This presentation indicates the difference of how to
realize OOP method between python and other OOP
language
Compare Pythons OOP methods with other OOP
languages. Analyze their advantages and disadvantages.
Whats Python?
Python is a general-purpose, interpreted high-level
programming language.
Its syntax is clear and emphasize readability.
Advantages of Python
Simple
Easy to study
Free and open source
High-level programming language
Portability
Expansibility
Embedability
Large and comprehensive standard libraries
Canonical code
Description
__dict__
__doc__
__name__
Class name
__module__
__bases__
Constructor: __init__()
The __init__ method is run as soon as an object of a class
is instantiated. Its aim is to initialize the object.
From the code , we can see that
after instantiate object, it
automatically invokes __init__()
As a result, it runs
self.name = Yang Li,
and
print self.name
Inheritance
Inheritance in Python is simple,
Just like JAVA, subclass can invoke
Attributes and methods in superclass.
From the example, Class Man inherits
Class Person, and invoke speak() method
In Class Person
Inherit Syntax:
class subclass(superclass):
Multiple Inheritance
Python supports a limited form of multiple inheritance.
A class definition with multiple base classes looks as follows:
Self
Self in Python is like the pointer this in C++. In
Python, functions in class access data via self.
An Example of Private
Public variable
Private variable
Polymorphism
Polymorphism is an important definition in OOP.
Absolutely, we can realize polymorphism in Python just
like in JAVA. I call it traditional polymorphism
In the next slide, there is an example of polymorphism in
Python.
But in Python,
Dynamic language
Polymorphism
Track variables types
Polymorphism
Conclusion
As a OOP language, Python has its special advantages but
also has its disadvantages.
Python can support operator overloading and multiple
inheritance etc. advanced definition that some OOP languages
dont have.
The advantages for Python to use design pattern is that it
supports dynamic type binding. In other words, an object is
rarely only one instance of a class, it can be dynamically
changed at runtime.
But Python might ignore a basic regulation of OOP: data and
methods hiding. It doesnt have the keywords of private,
public and protected to better support encapsulation. But I
think it aims to guarantee syntax simple. As the inventor of
Python, Guido Van Rossum said: abundant syntax bring
more burden than help.