SlideShare a Scribd company logo
1 of 16
Download to read offline
Slightly Advanced Python
    Chai,Software and Samosa
Python's calling mechanism
● Everything is an object

● Variables are essentially names assigned to
  objects

● Variables are not containers in memory

● Call by value or call by reference?
Python's calling mechanism contd..
● Call by sharing/Call by object/Call by object
  reference.

● Derived from a programming language
  called CLU.

"We call the argument passing technique _call by sharing_, because the
argument objects are shared between the caller and the called routine. This
technique does not correspond to most traditional argument passing techniques
(it is similar to argument passing in LISP). In particular it is not call by value
because mutations of arguments per-formed by the called routine will be visible
to the caller. And it is not call by reference because access is not given to the
variables of the caller, but merely to certain objects."
Decorators
● In python , Functions are first class citizens

● They can be passed as an argument to a
  function and returned from a function.

● Decorators are syntactic sugar.

● functools.wraps
Iterables and Iterators
Wrong ways of looping in python
  1. Using while
  2. Using range(len()) with indices

The pythonic way
  for x in iterable:

How does this work?
Iterators and Iterables
How to define a custom iterator for the class?

Difference between iterables and iterators?

Why use an iterator?

Why not use an iterator?
Generators
Most commonly used as a easy way of defining
iterators

Yields control to the callee but keeps record of
the local variables and point of execution

Resumes from where it left off on the next call.

Generators as coroutines
Useful functions for working with
iterators and iterables
● enumerate for seeing both indices and value
● iterkeys for seeing both keys and values
● itertools
  ○    chain for combining various iterators
  ○    tee for creating mutliple iterators from a single
    iterable
  ○     izip , just like zip but returns an iterator
Magic Methods
A way to mimic python's built in objects

Classes of Magic Methods:
1. Object creation and Destruction
2. Magic Methods for Comparisons
3. Arithmetic operators:
   ●      Unary Operators
   ●      Normal Arithmetic Operators
  ●       Reflected Arithmetic Operators
  ●       Augmented Assignment
Magic Methods(contd..)

4. Type conversion magic methods
5. Representing your classes(__str__,__repr__,
__hash__,__nonzero__, etc.)
Magic Methods (contd.- Controlling
Attribute Access)
● __getattr__, __setattr__, __delattr__,
  __getatrribute__

● Useful to define access control on attributes

● Care to be taken to avoid infinite recursion
Magic Methods (Writing your own
container)
● __len__, __getitem__, __setitem__,
  __delitem__, __iter__ , __reversed__ ,
  __contains__, __missing__

● Writing your own container class
Magic Methods (Wrapping up)
● Make your class instances behave like
  functions by defining the magic method
  __call__

● Context Managers for use with the with
  keyword.(__enter__ , __exit__ methods)

● Other magic methods for defining copying
  operations,pickling , descriptors etc.
VirtualEnv
● Tool to create isolated python environments.

● Great to test new versions of libraries and
  re-creating production environments

● Using virtualenv
   ○    Install virtualenv and virtualenvwrapper through
     pip
   ○ Add these two lines to your bashrc
       export WORKON_HOME=$HOME/.virtualenvs
       source /usr/local/bin/virtualenvwrapper.sh
Virtual Env commands
1. mkvirtualenv
2. rmvirtualenv
3. workon
4. deactivate
References
[1]http://effbot.org/zone/call-by-object.htm

[2]http://www.jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-
callbyreference-neither/

[3]http://www.rafekettler.com/magicmethods.html

[4]http://www.silverwareconsulting.com/index.cfm/2012/7/24/Getting-Started-
with-virtualenv-and-virtualenvwrapper-in-Python

[5]http://excess.org/article/2013/02/itergen1/

More Related Content

What's hot

Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic courseTran Khoa
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsRanel Padon
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introductionsandeep54552
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 
OOP interview questions & answers.
OOP interview questions & answers.OOP interview questions & answers.
OOP interview questions & answers.Questpond
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithmsNico Ludwig
 
Paca oops slid
Paca oops slidPaca oops slid
Paca oops slidpacatarpit
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic JavascriptBunlong Van
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objectsMahmoud Alfarra
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++sandeep54552
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variablesteach4uin
 
Datatype in JavaScript
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScriptRajat Saxena
 
DIWE - Programming with JavaScript
DIWE - Programming with JavaScriptDIWE - Programming with JavaScript
DIWE - Programming with JavaScriptRasan Samarasinghe
 

What's hot (20)

C++
C++C++
C++
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
 
Oops
OopsOops
Oops
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
OOP interview questions & answers.
OOP interview questions & answers.OOP interview questions & answers.
OOP interview questions & answers.
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithms
 
Paca oops slid
Paca oops slidPaca oops slid
Paca oops slid
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic Javascript
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objects
 
Java basic concept
Java basic conceptJava basic concept
Java basic concept
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Datatype in JavaScript
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScript
 
DIWE - Programming with JavaScript
DIWE - Programming with JavaScriptDIWE - Programming with JavaScript
DIWE - Programming with JavaScript
 

Viewers also liked

Desarrollo Local
Desarrollo LocalDesarrollo Local
Desarrollo Localguestc68e84
 
Love for science or 'Academic Prostitution' - DFD2014 version
Love for science or 'Academic Prostitution' - DFD2014 versionLove for science or 'Academic Prostitution' - DFD2014 version
Love for science or 'Academic Prostitution' - DFD2014 versionLourdes Verdes-Montenegro
 
Canube. Ciencia abierta en la nube. Tarea 3.2 biocomputación
Canube. Ciencia abierta en la nube. Tarea 3.2 biocomputación Canube. Ciencia abierta en la nube. Tarea 3.2 biocomputación
Canube. Ciencia abierta en la nube. Tarea 3.2 biocomputación Arantxa Muñoz Pérez del Pulgar
 
Design for Hypermedia - Class 09 (2015B)
Design for Hypermedia - Class 09 (2015B)Design for Hypermedia - Class 09 (2015B)
Design for Hypermedia - Class 09 (2015B)Bryan Chung
 
Open Science and Executable Papers
Open Science and Executable PapersOpen Science and Executable Papers
Open Science and Executable PapersJose Enrique Ruiz
 
Capital Social Y Redes
Capital Social Y RedesCapital Social Y Redes
Capital Social Y Redesguestc68e84
 

Viewers also liked (7)

Comunicado
ComunicadoComunicado
Comunicado
 
Desarrollo Local
Desarrollo LocalDesarrollo Local
Desarrollo Local
 
Love for science or 'Academic Prostitution' - DFD2014 version
Love for science or 'Academic Prostitution' - DFD2014 versionLove for science or 'Academic Prostitution' - DFD2014 version
Love for science or 'Academic Prostitution' - DFD2014 version
 
Canube. Ciencia abierta en la nube. Tarea 3.2 biocomputación
Canube. Ciencia abierta en la nube. Tarea 3.2 biocomputación Canube. Ciencia abierta en la nube. Tarea 3.2 biocomputación
Canube. Ciencia abierta en la nube. Tarea 3.2 biocomputación
 
Design for Hypermedia - Class 09 (2015B)
Design for Hypermedia - Class 09 (2015B)Design for Hypermedia - Class 09 (2015B)
Design for Hypermedia - Class 09 (2015B)
 
Open Science and Executable Papers
Open Science and Executable PapersOpen Science and Executable Papers
Open Science and Executable Papers
 
Capital Social Y Redes
Capital Social Y RedesCapital Social Y Redes
Capital Social Y Redes
 

Similar to C,s&s

Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiNico Ludwig
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Elizabeth Smith
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3Ahmet Bulut
 
Method Handles in Java
Method Handles in JavaMethod Handles in Java
Method Handles in Javahendersk
 
python interview prep question , 52 questions
python interview prep question , 52 questionspython interview prep question , 52 questions
python interview prep question , 52 questionsgokul174578
 
Python iteration
Python iterationPython iteration
Python iterationdietbuddha
 
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”GlobalLogic Ukraine
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternNitin Bhide
 
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina ZakharenkoElegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina ZakharenkoNina Zakharenko
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answerskavinilavuG
 
Programming in python - Week 2
Programming in python - Week 2Programming in python - Week 2
Programming in python - Week 2Priya Nayak
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answersRojaPriya
 
SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09Elizabeth Smith
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming LanguageMansiSuthar3
 
Stateful traits in Pharo
Stateful traits in PharoStateful traits in Pharo
Stateful traits in PharoESUG
 

Similar to C,s&s (20)

Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iii
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09
 
Advance python
Advance pythonAdvance python
Advance python
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
Method Handles in Java
Method Handles in JavaMethod Handles in Java
Method Handles in Java
 
python interview prep question , 52 questions
python interview prep question , 52 questionspython interview prep question , 52 questions
python interview prep question , 52 questions
 
Python master class 2
Python master class 2Python master class 2
Python master class 2
 
Python iteration
Python iterationPython iteration
Python iteration
 
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
 
Functions
FunctionsFunctions
Functions
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
 
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina ZakharenkoElegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Programming in python - Week 2
Programming in python - Week 2Programming in python - Week 2
Programming in python - Week 2
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming Language
 
Hibernate 1x2
Hibernate 1x2Hibernate 1x2
Hibernate 1x2
 
Stateful traits in Pharo
Stateful traits in PharoStateful traits in Pharo
Stateful traits in Pharo
 

C,s&s

  • 1. Slightly Advanced Python Chai,Software and Samosa
  • 2. Python's calling mechanism ● Everything is an object ● Variables are essentially names assigned to objects ● Variables are not containers in memory ● Call by value or call by reference?
  • 3. Python's calling mechanism contd.. ● Call by sharing/Call by object/Call by object reference. ● Derived from a programming language called CLU. "We call the argument passing technique _call by sharing_, because the argument objects are shared between the caller and the called routine. This technique does not correspond to most traditional argument passing techniques (it is similar to argument passing in LISP). In particular it is not call by value because mutations of arguments per-formed by the called routine will be visible to the caller. And it is not call by reference because access is not given to the variables of the caller, but merely to certain objects."
  • 4. Decorators ● In python , Functions are first class citizens ● They can be passed as an argument to a function and returned from a function. ● Decorators are syntactic sugar. ● functools.wraps
  • 5. Iterables and Iterators Wrong ways of looping in python 1. Using while 2. Using range(len()) with indices The pythonic way for x in iterable: How does this work?
  • 6. Iterators and Iterables How to define a custom iterator for the class? Difference between iterables and iterators? Why use an iterator? Why not use an iterator?
  • 7. Generators Most commonly used as a easy way of defining iterators Yields control to the callee but keeps record of the local variables and point of execution Resumes from where it left off on the next call. Generators as coroutines
  • 8. Useful functions for working with iterators and iterables ● enumerate for seeing both indices and value ● iterkeys for seeing both keys and values ● itertools ○ chain for combining various iterators ○ tee for creating mutliple iterators from a single iterable ○ izip , just like zip but returns an iterator
  • 9. Magic Methods A way to mimic python's built in objects Classes of Magic Methods: 1. Object creation and Destruction 2. Magic Methods for Comparisons 3. Arithmetic operators: ● Unary Operators ● Normal Arithmetic Operators ● Reflected Arithmetic Operators ● Augmented Assignment
  • 10. Magic Methods(contd..) 4. Type conversion magic methods 5. Representing your classes(__str__,__repr__, __hash__,__nonzero__, etc.)
  • 11. Magic Methods (contd.- Controlling Attribute Access) ● __getattr__, __setattr__, __delattr__, __getatrribute__ ● Useful to define access control on attributes ● Care to be taken to avoid infinite recursion
  • 12. Magic Methods (Writing your own container) ● __len__, __getitem__, __setitem__, __delitem__, __iter__ , __reversed__ , __contains__, __missing__ ● Writing your own container class
  • 13. Magic Methods (Wrapping up) ● Make your class instances behave like functions by defining the magic method __call__ ● Context Managers for use with the with keyword.(__enter__ , __exit__ methods) ● Other magic methods for defining copying operations,pickling , descriptors etc.
  • 14. VirtualEnv ● Tool to create isolated python environments. ● Great to test new versions of libraries and re-creating production environments ● Using virtualenv ○ Install virtualenv and virtualenvwrapper through pip ○ Add these two lines to your bashrc export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
  • 15. Virtual Env commands 1. mkvirtualenv 2. rmvirtualenv 3. workon 4. deactivate