SlideShare ist ein Scribd-Unternehmen logo
1 von 65
Shao-Chuan Wang
Presentation
version 2.5.4
 Python Object and Types
 Object and Class Model
 Name Scope Object Life Cycle
 Exception Handling
Everything is an object!
 New-style object v.s. Old-Style Objects
 The following presentation mainly focuses
on
new-style objects.
 Type checking at compile-time V.S. run-time
◦ Statically typed Dynamically typed
int a a
 Explicit V.S. Implicit type conversion
◦ Strongly-typed v.s. weakly-typed
 Python is dynamically and strongly typed
 <type ‘object’> is an instance of <type ‘type’>
 <type ‘type’> is an instance of itself
 <type ‘type’> is a subclass/subtype of <type
‘object’>
<type ‘type’>
type of all types
<type ‘object’>
bases of all other types
• New a type object by
subtyping
• New an object by explicit
instantiation
• The type object serves as a
factory that can churn out
new objects (to each of which
it is related as type-instance).
This is why every object has
a type.
<type ‘type’>
type of all types
<type ‘object’>
bases of all other types
<class ‘C’>
• Generate a ‘class’ at runtime.
• Generate a ‘function’ at runtime.
• Very flexible and generic
 They are objects.
 Not All (but most of) type objects are able to
create instance objects via the static method
__new__.
◦ type(typeObj) -> <type ‘type’>
◦ callable(typeObj) -> True
 Not all (but most of) type object are
subclassable (e.g. FunctionType)
Enhancing productivity and readability
• All Python objects have:
– an identity (an integer, returned by id(x))
– a type (returned by type(x))
– some content/value
• Once the object is created, you cannot change
the type nor the identity. You may or may not
change the content of the object.
– Mutable object: objects whose content/value can
change
– Immutable object: objects whose content/value is
unchangeable once they are created
• ‘string’ is an object of type <type ‘str’>, and
has a unique id.
 Names refer to objects; names are the ‘keys’
to obtain the objects. (a little bit similar to
reference type in C)
 An object can have many names, or no name
at all.
 Names live in namespaces (introduced in the
next slide)
 They are not the properties of the object
• Namespaces are collections of (name, object
reference) pairs (implemented using
dictionaries).
• globals(): the namespace of the __main__
module
• The built-in namespace associated with the
execution of a code block is actually found by
looking up the name __builtins__ in its global
namespace
• Every object can have its namespace. (e.g. a
module namespace, an instance namespace, a
function’s local namespace)
 An attribute is a way to get from one object to
another, by applying the dot operation
◦ objectName.attrName -> anotherObject
◦ objectName.attrName 
objectName.__getattribute__(‘attrName')
 The object and anotherObject can be of any kind, including
module, class, function/method, and all other kinds of
instance objects.
 The vague hierarchy of ‘class’ and ‘object’. Class B can
include object a, and object a now can also include Class B.
• The object must be created or already exist
before assignment
• objectName.attrName = anotherObj 
objectName.__setattr__(‘attrName’,
anotherObj)
• What does an assignment really do?
– Binding the name, and the object
– Adding a new attribute to the ‘local’ namespace
• Assignment one object to another object is like
copying its reference to the name. (in terms of
C language)
• It actually re-bind the name and the object, so
the object now has several names
Python: C/C++:
a: 0 at 0013FEE0 b: 5 at 0013FEDC
a: 5 at 0013FEE0 b: 5 at 0013FEDC
# before a = b
{‘a’:0, ‘b’: 5}
# after a = b
{‘a’:5, ‘b’: 5}
• Names are ‘references’ (implemented by
PyObject * in C) to objects.
• Assignment a literal integer object (the value
larger) to a name is to allocate a new memory
containing that value and return the reference.
Python: C/C++:
0013FEE0
0013FEE0
 Creating an instance of a Class by calling
__new__(cls, …)
 There is no way to create a new instance
without calling __new__
 cls. __new__() is a static method
 Descriptor definition: An object with a __get__() method,
and optionally __set__() and __delete__() methods,
accepting specific parameters is said to follow the
descriptor protocol.
1. d.__get__(cobj, C)
2. d.__set__(cobj, 5)
3. d.__delete__(cobj)
 Data Descriptor:
◦ A descriptor with
both __get__() and
__set__() methods
 Non-data Descriptor
◦ Descriptors with
only the __get__()
method.
- Can be hidden by
Instance attribute.
 A function is actually a non-data descriptor.
 An easier way to generate a descriptor:
property
• Class function and instance method are
actually different objects
• cobj.f => C.f.__get__(cobj, C)
• cobj.d gets the return value of __get__,
implemented in the descriptor class.
• C.__dict__[‘d’] gets the descriptor object.
• They are of course different (in general).
 The ‘local’ namespace (mapping of ‘name’ and ‘object
reference’) is actually saved in __dict__
• obj.attrName -> invoking obj.__getattribute__(‘attrName’)
– If attrName is a Python-provided attribute, return it.
– Search obj.__class__.__dict__ for attrName . If it exists
and is a data-descriptor, return the descriptor result.
Search all bases of obj.__class__ for the same case.
– Search obj.__dict__ for attrName , and return if found.
If obj is a type object, search its bases too. If it is a
type object and a descriptor is found in the object or
its bases, return the descriptor result.
– Search obj.__class__.__dict__ for attrName . If it exists
and is a non-data descriptor, return the descriptor
result. If it exists, and is not a descriptor, just return it.
Search all bases of objectname.__class__ for same case.
– Raise AttributeError
 What if Multi-Inheritance?
c.a -> ???
d.a -> ???
Why?
• Searching attributes in the order of __mro__
• __mro__ is a data descriptor of <type ‘type’>
• mro: Method Resolution Order
 A more complex example
e.a -> ???
Be careful about multi-inheritance and naming
conflict
• If B is a subclass of A, then B should always
appear before A in all __mro__s. Let’s denote
B > A
• If C appears before D in the list of bases in a
class statement (e.g. Z(C, D)), then C > D
MRO computing principle:
• If E > F in one scenario (or one __mro__), then
it should be that E > F in all scenarios (or all
__mro__s). A: X > Y, B: Y > X,
so when C(A, B) => Conflict
 L[E] = E + Merge(DBAO, CAO, DC)
= E + D + Merge(BA, CAO, C)
= E + D + B + Merge(A,CAO,C)
= E + D + B + C + Merge(A,AO)
= E + D + B + C + A + O
Object
B A
D C
E
• Call by reference?
•Why?
 Assignment is to re-bind the name and
object in the ‘local’ namespace. In the case,
the object names in the function are saved in
func_code.co_varnames, and
func_code.co_argcount.
 Assignment cannot change the content of the
object.
• Calling method of the argument object in the function:
1
2
4
1
2
2
1
2
3
or
???
1
2
3
1
2
1
error when invoking!!
3
5
1
6
• When returning and passing the function. Be careful
that the name conflict may cause the problem.
• Binding objects in
func_closure when
used object in the
enclosing area.
• Scopes are
determined
statically
• Built-in namespace is created when the Python
interpreter starts up, and is never deleted.
• The global namespace for a module is created
when the module definition is read in; normally,
module namespaces also last until the interpreter
quits
• The local namespace for a function is created
when the function is called, and deleted when the
function returns or raises an exception that is not
handled within the function. recursive
invocations each have their own local namespace
• import moduleName: the module object comes into
the global dictionary.
• from moduleName import someObject: the some
object comes into the global dictionary.
 Encapsulation
◦ Public object access, but read-only by default!
◦ Outer inner scope cannot change the inner outer
scope object’s value if programmer does not
provide the interfaces methods.
◦ The only way to change the content value of the
object is to through the object itself.
File write completed
successfully
If file exists, and
open successfully
Unexpected error
• “Catching StandardError” or “Unable to open file”?
 Exception typically is a class object (of type
<type ‘type’>)
IOError
EnvironmentError TypeError
Exception
BaseException
object
StandardError
LookupError
KeyError
Exception
Inheritance
Tree
StopIterationSystemExit
 All built-in exception objects are defined in
exceptions module
 When an exception is not handled at all, the
interpreter terminates execution of the
program, or returns to its interactive main loop
 Let caller to handle the exception
 raise ‘string’, ‘value’
 raise ExCObject, ‘value’
 raise ExCObject(‘value’)
exInst = ExCObject(‘value’)
 raise exInst
 raise exInstObj
 In past versions of Python string exceptions were
supported. In Python 1.5 and newer versions, all
standard exceptions have been converted to class
objects and users are encouraged to do the same.
String exceptions will raise a DeprecationWarning
in Python 2.5 and newer. In future versions,
support for string exceptions will be removed.
 We can actually catch Exception instance, and handle it.
 Take IOError for example, it has args, errno, filename, message,
strerror attributes, they are all data descriptors
 args defined in BaseException
 errno, filename, message, strerror defined in EnvironmentError
• Inherit ‘Exception’ class object:
• repr() returns the canonical string representation of the object.
• __str__() is called by the str built-in function and by the print
statement to compute the informal string representation of an
object
 What is the output?
• L[C] = C + D + EOF + IO + ENV + S
S
EOF Env
D IO
C
Avoid Multi-inheritance
From different Exception
Type Objects
• The problem with this code is that it leaves the file
open for an indeterminate amount of time after
the code has finished executing. This is not an
issue in simple scripts, but can be a problem for
larger applications.
• After the statement is executed, the file f is always
closed, even if a problem was encountered while
processing the lines.
• The above code roughly translates into this:
• var.__exit__() records predefined clean-up actions
http://www.python.org/dev/peps/pep-0343/
Other tricks about Python
 Difference between a & b ?
 a save in A.__dict__, and b save in
instanceOfA.__dict__
 To rebind a’s value in A, only by Class A
object, cannot through any instances of A.
• List comprehension
– s = [x ** 2 for x in range(10)]
– v = [2 ** x for x in range(10)]
– zeros = [0 for I in range(10)]
• Iterator & Generator
– g = (x * 2 for x in range(1000)) => g.next(), g.next(),
g.next()
– a function which can stop whatever it is doing at an
arbitrary point in its body, return a value back to the
caller, and, later on, resume from the point it had
`frozen'
 ‘yield’ statement
• Loop read a file:
without
occupying too
much memory.
 Generator application: Permutation
generator function: return a generator
implicitly v = s.next()
=> call/resume the generator function call
boundary condition
Thank you!

Weitere ähnliche Inhalte

Was ist angesagt?

Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with pythonArslan Arshad
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized军 沈
 
Using Xcore with Xtext
Using Xcore with XtextUsing Xcore with Xtext
Using Xcore with XtextHolger Schill
 
Xtext's new Formatter API
Xtext's new Formatter APIXtext's new Formatter API
Xtext's new Formatter APImeysholdt
 
Getting started with the JNI
Getting started with the JNIGetting started with the JNI
Getting started with the JNIKirill Kounik
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptNascenia IT
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programmingSrinivas Narasegouda
 
Java Script Patterns
Java Script PatternsJava Script Patterns
Java Script PatternsAllan Huang
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysDevaKumari Vijay
 
Learn Ruby by Reading the Source
Learn Ruby by Reading the SourceLearn Ruby by Reading the Source
Learn Ruby by Reading the SourceBurke Libbey
 

Was ist angesagt? (20)

Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Prototype
PrototypePrototype
Prototype
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
Using Xcore with Xtext
Using Xcore with XtextUsing Xcore with Xtext
Using Xcore with Xtext
 
Xtext's new Formatter API
Xtext's new Formatter APIXtext's new Formatter API
Xtext's new Formatter API
 
Getting started with the JNI
Getting started with the JNIGetting started with the JNI
Getting started with the JNI
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Design patterns(red)
Design patterns(red)Design patterns(red)
Design patterns(red)
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Java Script Patterns
Java Script PatternsJava Script Patterns
Java Script Patterns
 
Java
JavaJava
Java
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
Xtext Eclipse Con
Xtext Eclipse ConXtext Eclipse Con
Xtext Eclipse Con
 
The Xtext Grammar Language
The Xtext Grammar LanguageThe Xtext Grammar Language
The Xtext Grammar Language
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Learn Ruby by Reading the Source
Learn Ruby by Reading the SourceLearn Ruby by Reading the Source
Learn Ruby by Reading the Source
 

Ähnlich wie About Python

Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptxSAICHARANREDDYN
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep DiveManish Jangir
 
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITKPankaj Prateek
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programmingNeelesh Shukla
 
Ios development
Ios developmentIos development
Ios developmentelnaqah
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in PharoPharo
 
C questions
C questionsC questions
C questionsparm112
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیMohammad Reza Kamalifard
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objectsmcollison
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorialGhulam Abbas Khan
 
Master in javascript
Master in javascriptMaster in javascript
Master in javascriptRobbin Zhao
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 

Ähnlich wie About Python (20)

Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
 
Dynamic Python
Dynamic PythonDynamic Python
Dynamic Python
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
 
Java tutorial part 3
Java tutorial part 3Java tutorial part 3
Java tutorial part 3
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
106da session5 c++
106da session5 c++106da session5 c++
106da session5 c++
 
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
Ios development
Ios developmentIos development
Ios development
 
Oop java
Oop javaOop java
Oop java
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
C questions
C questionsC questions
C questions
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
Master in javascript
Master in javascriptMaster in javascript
Master in javascript
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 

Mehr von Shao-Chuan Wang

Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine LearningShao-Chuan Wang
 
Beyond The Euclidean Distance: Creating effective visual codebooks using the ...
Beyond The Euclidean Distance: Creating effective visual codebooks using the ...Beyond The Euclidean Distance: Creating effective visual codebooks using the ...
Beyond The Euclidean Distance: Creating effective visual codebooks using the ...Shao-Chuan Wang
 
A Friendly Guide To Sparse Coding
A Friendly Guide To Sparse CodingA Friendly Guide To Sparse Coding
A Friendly Guide To Sparse CodingShao-Chuan Wang
 
An Exemplar Model For Learning Object Classes
An Exemplar Model For Learning Object ClassesAn Exemplar Model For Learning Object Classes
An Exemplar Model For Learning Object ClassesShao-Chuan Wang
 
Evaluation Of Color Descriptors For Object And Scene
Evaluation Of Color Descriptors For Object And SceneEvaluation Of Color Descriptors For Object And Scene
Evaluation Of Color Descriptors For Object And SceneShao-Chuan Wang
 
Spatially Coherent Latent Topic Model For Concurrent Object Segmentation and ...
Spatially Coherent Latent Topic Model For Concurrent Object Segmentation and ...Spatially Coherent Latent Topic Model For Concurrent Object Segmentation and ...
Spatially Coherent Latent Topic Model For Concurrent Object Segmentation and ...Shao-Chuan Wang
 
Image Classification And Support Vector Machine
Image Classification And Support Vector MachineImage Classification And Support Vector Machine
Image Classification And Support Vector MachineShao-Chuan Wang
 

Mehr von Shao-Chuan Wang (10)

Book Cover Recognition
Book Cover RecognitionBook Cover Recognition
Book Cover Recognition
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 
Beyond The Euclidean Distance: Creating effective visual codebooks using the ...
Beyond The Euclidean Distance: Creating effective visual codebooks using the ...Beyond The Euclidean Distance: Creating effective visual codebooks using the ...
Beyond The Euclidean Distance: Creating effective visual codebooks using the ...
 
Self Taught Learning
Self Taught LearningSelf Taught Learning
Self Taught Learning
 
A Friendly Guide To Sparse Coding
A Friendly Guide To Sparse CodingA Friendly Guide To Sparse Coding
A Friendly Guide To Sparse Coding
 
An Exemplar Model For Learning Object Classes
An Exemplar Model For Learning Object ClassesAn Exemplar Model For Learning Object Classes
An Exemplar Model For Learning Object Classes
 
Evaluation Of Color Descriptors For Object And Scene
Evaluation Of Color Descriptors For Object And SceneEvaluation Of Color Descriptors For Object And Scene
Evaluation Of Color Descriptors For Object And Scene
 
Spatially Coherent Latent Topic Model For Concurrent Object Segmentation and ...
Spatially Coherent Latent Topic Model For Concurrent Object Segmentation and ...Spatially Coherent Latent Topic Model For Concurrent Object Segmentation and ...
Spatially Coherent Latent Topic Model For Concurrent Object Segmentation and ...
 
Support Vector Machine
Support Vector MachineSupport Vector Machine
Support Vector Machine
 
Image Classification And Support Vector Machine
Image Classification And Support Vector MachineImage Classification And Support Vector Machine
Image Classification And Support Vector Machine
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Kürzlich hochgeladen (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

About Python

  • 2.  Python Object and Types  Object and Class Model  Name Scope Object Life Cycle  Exception Handling
  • 4.  New-style object v.s. Old-Style Objects  The following presentation mainly focuses on new-style objects.
  • 5.  Type checking at compile-time V.S. run-time ◦ Statically typed Dynamically typed int a a  Explicit V.S. Implicit type conversion ◦ Strongly-typed v.s. weakly-typed  Python is dynamically and strongly typed
  • 6.  <type ‘object’> is an instance of <type ‘type’>  <type ‘type’> is an instance of itself  <type ‘type’> is a subclass/subtype of <type ‘object’> <type ‘type’> type of all types <type ‘object’> bases of all other types
  • 7. • New a type object by subtyping • New an object by explicit instantiation • The type object serves as a factory that can churn out new objects (to each of which it is related as type-instance). This is why every object has a type. <type ‘type’> type of all types <type ‘object’> bases of all other types <class ‘C’>
  • 8. • Generate a ‘class’ at runtime. • Generate a ‘function’ at runtime. • Very flexible and generic
  • 9.  They are objects.  Not All (but most of) type objects are able to create instance objects via the static method __new__. ◦ type(typeObj) -> <type ‘type’> ◦ callable(typeObj) -> True  Not all (but most of) type object are subclassable (e.g. FunctionType)
  • 11. • All Python objects have: – an identity (an integer, returned by id(x)) – a type (returned by type(x)) – some content/value • Once the object is created, you cannot change the type nor the identity. You may or may not change the content of the object. – Mutable object: objects whose content/value can change – Immutable object: objects whose content/value is unchangeable once they are created • ‘string’ is an object of type <type ‘str’>, and has a unique id.
  • 12.  Names refer to objects; names are the ‘keys’ to obtain the objects. (a little bit similar to reference type in C)  An object can have many names, or no name at all.  Names live in namespaces (introduced in the next slide)  They are not the properties of the object
  • 13. • Namespaces are collections of (name, object reference) pairs (implemented using dictionaries). • globals(): the namespace of the __main__ module • The built-in namespace associated with the execution of a code block is actually found by looking up the name __builtins__ in its global namespace • Every object can have its namespace. (e.g. a module namespace, an instance namespace, a function’s local namespace)
  • 14.  An attribute is a way to get from one object to another, by applying the dot operation ◦ objectName.attrName -> anotherObject ◦ objectName.attrName  objectName.__getattribute__(‘attrName')  The object and anotherObject can be of any kind, including module, class, function/method, and all other kinds of instance objects.  The vague hierarchy of ‘class’ and ‘object’. Class B can include object a, and object a now can also include Class B.
  • 15. • The object must be created or already exist before assignment • objectName.attrName = anotherObj  objectName.__setattr__(‘attrName’, anotherObj) • What does an assignment really do? – Binding the name, and the object – Adding a new attribute to the ‘local’ namespace
  • 16. • Assignment one object to another object is like copying its reference to the name. (in terms of C language) • It actually re-bind the name and the object, so the object now has several names Python: C/C++: a: 0 at 0013FEE0 b: 5 at 0013FEDC a: 5 at 0013FEE0 b: 5 at 0013FEDC # before a = b {‘a’:0, ‘b’: 5} # after a = b {‘a’:5, ‘b’: 5}
  • 17. • Names are ‘references’ (implemented by PyObject * in C) to objects. • Assignment a literal integer object (the value larger) to a name is to allocate a new memory containing that value and return the reference. Python: C/C++: 0013FEE0 0013FEE0
  • 18.  Creating an instance of a Class by calling __new__(cls, …)  There is no way to create a new instance without calling __new__  cls. __new__() is a static method
  • 19.  Descriptor definition: An object with a __get__() method, and optionally __set__() and __delete__() methods, accepting specific parameters is said to follow the descriptor protocol. 1. d.__get__(cobj, C) 2. d.__set__(cobj, 5) 3. d.__delete__(cobj)
  • 20.  Data Descriptor: ◦ A descriptor with both __get__() and __set__() methods  Non-data Descriptor ◦ Descriptors with only the __get__() method. - Can be hidden by Instance attribute.
  • 21.  A function is actually a non-data descriptor.  An easier way to generate a descriptor: property
  • 22. • Class function and instance method are actually different objects • cobj.f => C.f.__get__(cobj, C)
  • 23. • cobj.d gets the return value of __get__, implemented in the descriptor class. • C.__dict__[‘d’] gets the descriptor object. • They are of course different (in general).
  • 24.  The ‘local’ namespace (mapping of ‘name’ and ‘object reference’) is actually saved in __dict__
  • 25. • obj.attrName -> invoking obj.__getattribute__(‘attrName’) – If attrName is a Python-provided attribute, return it. – Search obj.__class__.__dict__ for attrName . If it exists and is a data-descriptor, return the descriptor result. Search all bases of obj.__class__ for the same case. – Search obj.__dict__ for attrName , and return if found. If obj is a type object, search its bases too. If it is a type object and a descriptor is found in the object or its bases, return the descriptor result. – Search obj.__class__.__dict__ for attrName . If it exists and is a non-data descriptor, return the descriptor result. If it exists, and is not a descriptor, just return it. Search all bases of objectname.__class__ for same case. – Raise AttributeError
  • 26.  What if Multi-Inheritance? c.a -> ??? d.a -> ??? Why?
  • 27. • Searching attributes in the order of __mro__ • __mro__ is a data descriptor of <type ‘type’> • mro: Method Resolution Order
  • 28.  A more complex example e.a -> ??? Be careful about multi-inheritance and naming conflict
  • 29. • If B is a subclass of A, then B should always appear before A in all __mro__s. Let’s denote B > A • If C appears before D in the list of bases in a class statement (e.g. Z(C, D)), then C > D MRO computing principle: • If E > F in one scenario (or one __mro__), then it should be that E > F in all scenarios (or all __mro__s). A: X > Y, B: Y > X, so when C(A, B) => Conflict
  • 30.  L[E] = E + Merge(DBAO, CAO, DC) = E + D + Merge(BA, CAO, C) = E + D + B + Merge(A,CAO,C) = E + D + B + C + Merge(A,AO) = E + D + B + C + A + O Object B A D C E
  • 31.
  • 32. • Call by reference? •Why?
  • 33.  Assignment is to re-bind the name and object in the ‘local’ namespace. In the case, the object names in the function are saved in func_code.co_varnames, and func_code.co_argcount.  Assignment cannot change the content of the object.
  • 34. • Calling method of the argument object in the function:
  • 38. • When returning and passing the function. Be careful that the name conflict may cause the problem. • Binding objects in func_closure when used object in the enclosing area. • Scopes are determined statically
  • 39. • Built-in namespace is created when the Python interpreter starts up, and is never deleted. • The global namespace for a module is created when the module definition is read in; normally, module namespaces also last until the interpreter quits • The local namespace for a function is created when the function is called, and deleted when the function returns or raises an exception that is not handled within the function. recursive invocations each have their own local namespace
  • 40. • import moduleName: the module object comes into the global dictionary. • from moduleName import someObject: the some object comes into the global dictionary.
  • 41.  Encapsulation ◦ Public object access, but read-only by default! ◦ Outer inner scope cannot change the inner outer scope object’s value if programmer does not provide the interfaces methods. ◦ The only way to change the content value of the object is to through the object itself.
  • 42.
  • 43. File write completed successfully If file exists, and open successfully Unexpected error
  • 44. • “Catching StandardError” or “Unable to open file”?
  • 45.  Exception typically is a class object (of type <type ‘type’>) IOError EnvironmentError TypeError Exception BaseException object StandardError LookupError KeyError Exception Inheritance Tree StopIterationSystemExit
  • 46.  All built-in exception objects are defined in exceptions module
  • 47.  When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop
  • 48.  Let caller to handle the exception
  • 49.
  • 50.  raise ‘string’, ‘value’  raise ExCObject, ‘value’  raise ExCObject(‘value’) exInst = ExCObject(‘value’)  raise exInst  raise exInstObj
  • 51.  In past versions of Python string exceptions were supported. In Python 1.5 and newer versions, all standard exceptions have been converted to class objects and users are encouraged to do the same. String exceptions will raise a DeprecationWarning in Python 2.5 and newer. In future versions, support for string exceptions will be removed.
  • 52.  We can actually catch Exception instance, and handle it.  Take IOError for example, it has args, errno, filename, message, strerror attributes, they are all data descriptors  args defined in BaseException  errno, filename, message, strerror defined in EnvironmentError
  • 53.
  • 54. • Inherit ‘Exception’ class object: • repr() returns the canonical string representation of the object. • __str__() is called by the str built-in function and by the print statement to compute the informal string representation of an object
  • 55.  What is the output?
  • 56. • L[C] = C + D + EOF + IO + ENV + S S EOF Env D IO C Avoid Multi-inheritance From different Exception Type Objects
  • 57. • The problem with this code is that it leaves the file open for an indeterminate amount of time after the code has finished executing. This is not an issue in simple scripts, but can be a problem for larger applications. • After the statement is executed, the file f is always closed, even if a problem was encountered while processing the lines.
  • 58. • The above code roughly translates into this: • var.__exit__() records predefined clean-up actions http://www.python.org/dev/peps/pep-0343/
  • 60.  Difference between a & b ?  a save in A.__dict__, and b save in instanceOfA.__dict__  To rebind a’s value in A, only by Class A object, cannot through any instances of A.
  • 61. • List comprehension – s = [x ** 2 for x in range(10)] – v = [2 ** x for x in range(10)] – zeros = [0 for I in range(10)] • Iterator & Generator – g = (x * 2 for x in range(1000)) => g.next(), g.next(), g.next() – a function which can stop whatever it is doing at an arbitrary point in its body, return a value back to the caller, and, later on, resume from the point it had `frozen'
  • 63. • Loop read a file: without occupying too much memory.
  • 64.  Generator application: Permutation generator function: return a generator implicitly v = s.next() => call/resume the generator function call boundary condition