Object-Oriented Design With Python

3y ago
60 Views
7 Downloads
1.42 MB
26 Pages
Last View : 23d ago
Last Download : 3m ago
Upload by : Hayden Brunner
Transcription

Object-Oriented Designwith PythonCSCI 5448: Object – Oriented A & DPresentationYang Li

Summary This presentation assumes audience have the knowledgeof Object-Oriented A & D and emphasize on OOPprogramming with python Introduces Python’s special methods to realize classdefinition, inheritance, multiple inheritance, accessibility,polymorphism, encapsulation. This presentation indicates the difference of how torealize OOP method between python and other OOPlanguage Compare Python’s OOP methods with other OOPlanguages. Analyze their advantages and disadvantages.

What’s Python? Python is a general-purpose, interpreted high-levelprogramming language. Its syntax is clear and emphasize readability. Python has a large and comprehensive standard library. Python supports multiple programming paradigms,primarily but not limited to object-oriented, imperativeand, to a lesser extent, functional programming styles. It features a fully dynamic type system and automaticmemory management

Advantages of Python SimpleEasy to studyFree and open sourceHigh-level programming languagePortabilityExpansibilityEmbedabilityLarge and comprehensive standard librariesCanonical code

A Example of Python ClassThis example includesclass definition, constructor function, destructor function,attributes and methods definition and object definition.These definitions and uses will be introduced specifically inthe following.

Class Definition and Object Instantiation Class definition syntax:class subclass[(superclass)]:[attributes and methods] Object instantiation syntax:object class() Attributes and methods invoke:object.attributeobject.method()

Special Class Attributes in Python Except for self-defined class attributes in Python, classhas some special attributes. They are provided by objectmodule.Attributes NameDescriptiondictDict variable of class name spacedocDocument reference string of classnameClass namemoduleModule name consisting of classbasesThe tuple including all the superclasses

Constructor: init () The init method is run as soon as an object of a classis instantiated. Its aim is to initialize the object.From the code , we can see thatafter instantiate object, itautomatically invokes init ()As a result, it runsself.name ‘Yang Li’,andprint self.name

Form and Object for Class Class includes two members: form and object. The example in the following can reflect what is thedifference between object and form for class.Invoke form: just invoke data ormethod in the class, so i 123Invoke object: instantialize objectFirstly, and then invoke data orMethods.Here it experienced init (),i 12345

InheritanceInheritance in Python is simple,Just like JAVA, subclass can invokeAttributes and methods in superclass.From the example, Class Man inheritsClass Person, and invoke speak() methodIn Class PersonInherit Syntax:class subclass(superclass): In Python, it supports multiple inheritance,In the next slide, it will be introduced.

Multiple Inheritance Python supports a limited form of multiple inheritance. A class definition with multiple base classes looks as follows:class DerivedClass(Base1, Base2, Base3 ) statement-1 statement-2 The only rule necessary to explain the semantics is theresolution rule used for class attribute references. This isdepth-first, left-to-right. Thus, if an attribute is not found inDerivedClass, it is searched in Base1, then recursively in theclasses of Base1, and only if it is not found there, it is searchedin Base2, and so on.

An Example of Multiple InheritanceC multiple-inherit A and B, butsince A is in the left of B, so Cinherit A and invoke A.A()according to the left-to-rightsequence.To implement C.B(), class Adoes not have B() method, soC inherit B for the secondpriority. So C.B() actuallyinvokes B() in class B.

“Self” “Self” in Python is like the pointer “this” in C . InPython, functions in class access data via “self”. “Self” in Python works as a variable of function but itwon’t invoke data.

Encapsulation – Accessibility (1) In Python, there is no keywords like ‘public’, ‘protected’and ‘private’ to define the accessibility. In other words, InPython, it acquiesce that all attributes are public. But there is a method in Python to define Private:Add “ ” in front of the variable and function namecan hide them when accessing them from out ofclass.

An Example of PrivatePublic variablePrivate variableInvoke private variable in classAccess public variable out of class, succeedAccess private variable our of class, failAccess public function but this function accessPrivate variable B successfully since they are inthe same class.

Encapsulation – Accessibility (2) Actually, the private accessibility method is just a rule,not the limitation of compiler. Its fact is to change name of private name like variableorfunction()toClassName variableorClassName function(). So we can’t access thembecause of wrong names. We even can use the special syntax to access the privatedata or methods. The syntax is actually its changedname. Refer to the following example in the next slide.

An example of Accessing PrivateDefine public functionDefine private functionAccess public functionCan’t access private functionAccess private function via changed name

Polymorphism Polymorphism is an important definition in OOP.Absolutely, we can realize polymorphism in Python justlike in JAVA. I call it “traditional polymorphism” In the next slide, there is an example of polymorphism inPython. But in Python,Only traditional polymorphism exist?

Compare Accessibility of Python and Java Java is a static and strong type definition language. Javahas strict definition of accessibility type with keywords. While Python is a dynamic and weak type definitionlanguage. Python acquiesces all the accessibility typesare public except for supporting a method to realizeprivate accessibility virtually. Someone think Python violates the requirement ofencapsulation. But its aim is to make language simple.

Traditional Polymorphism Example

Everywhere is polymorphism in Python (1) Since Python is a dynamic programming language, itmeans Python is strongly typed as the interpreter keepstrack of all variables types. It reflects the polymorphismcharacter in Python.Dynamic languagePolymorphismTrack variables types

Everywhere is polymorphism in Python (2) So, in Python, many operators have the property ofpolymorphism. Like the following example: Looks stupid, but the key is that variables can supportany objects which support ‘add’ operation. Not onlyinteger but also string, list, tuple and dictionary canrealize their relative ‘add’ operation.

Everywhere is polymorphism in Python (3) Some methods in Python also have polymorphismcharacter like ‘repr’ function. For ‘repr’ method, it can transfer any kinds of data tostring type. In the above example, it converts integer 123to string ‘123’ and it can even added to string c ‘string’ toget ‘123string’.

Avoid Destroying Polymorphism! Many operators and functions in Python are polymorphic. Soas long as you use the polymorphic operators and functions,polymorphism will exist even if you don’t have this purpose. The only way to destroy polymorphism is check types byusing functions like ‘type’, ‘isinstance’ and ‘issubclass’ etc. So in the programming, we should avoid using these methodswhich might destroy polymorphism except for compilerdesign. The most important thing is to let objects to work according toyour requirements rather than mind if they have right types

How to Affect icoperators andmethodsFunctions of‘type’,‘isinstance’,‘issubclass’ etcPolymorphism

Conclusion As a OOP language, Python has its special advantages butalso has its disadvantages. Python can support operator overloading and multipleinheritance etc. advanced definition that some OOP languagesdon’t have. The advantages for Python to use design pattern is that itsupports dynamic type binding. In other words, an object israrely only one instance of a class, it can be dynamicallychanged at runtime. But Python might ignore a basic regulation of OOP: data andmethods hiding. It doesn’t have the keywords of ‘private’,‘public’ and ‘protected’ to better support encapsulation. But Ithink it aims to guarantee syntax simple. As the inventor ofPython, Guido Van Rossum said: “abundant syntax bringmore burden than help”.

Python is a general-purpose, interpreted high-level programming language. Its syntax is clear and emphasize readability. Python has a large and comprehensive standard library. Python supports multiple programming paradigms, primarily but not limited to object-oriented, imperative and, to a lesser extent, functional programming .

Related Documents:

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter directly to write your programs. Python is Object-Oriented: Python supports Object-Oriented style or technique of programming that encapsulates code within objects. Python is a Beginner's Language: Python is a great language for the

site "Python 2.x is legacy, Python 3.x is the present and future of the language". In addition, "Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers". However, note that Python 2 is currently still rather widely used. Python 2 and 3 are about 90% similar. Hence if you learn Python 3, you will likely

There are currently two versions of Python in use; Python 2 and Python 3. Python 3 is not backward compatible with Python 2. A lot of the imported modules were only available in Python 2 for quite some time, leading to a slow adoption of Python 3. However, this not really an issue anymore. Support for Python 2 will end in 2020.

method dispatch in different object-oriented programming languages. We also include an appendix on object-oriented programming languages, in which we consider the distinction between object-based and object-oriented programming languages and the evolution and notation and process of object-oriented analysis and design, start with Chapters 5 and 6;

Object Class: Independent Protection Layer Object: Safety Instrumented Function SIF-101 Compressor S/D Object: SIF-129 Tower feed S/D Event Data Diagnostics Bypasses Failures Incidences Activations Object Oriented - Functional Safety Object: PSV-134 Tower Object: LT-101 Object Class: Device Object: XS-145 Object: XV-137 Object: PSV-134 Object .