SlideShare a Scribd company logo
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 (20)

PDF
Javascript basic course
Tran Khoa
 
PDF
Python Programming - VI. Classes and Objects
Ranel Padon
 
PPTX
C++ programming introduction
sandeep54552
 
PPTX
Oops
Jaya Kumari
 
PPTX
Variables in python
Jaya Kumari
 
PPTX
Polymorphism
prabhat kumar
 
PPTX
OOP interview questions & answers.
Questpond
 
ODP
(4) collections algorithms
Nico Ludwig
 
PPTX
Paca oops slid
pacatarpit
 
PPT
Basic Javascript
Bunlong Van
 
PDF
Inheritance
Pranali Chaudhari
 
PDF
C++ Object oriented concepts & programming
nirajmandaliya
 
PPTX
Chapter2 array of objects
Mahmoud Alfarra
 
PDF
Java basic concept
University of Potsdam
 
PPTX
Constructor and Destructors in C++
sandeep54552
 
PPT
Advanced JavaScript
Fu Cheng
 
PPTX
L2 datatypes and variables
teach4uin
 
ODP
Datatype in JavaScript
Rajat Saxena
 
PPSX
DIWE - Programming with JavaScript
Rasan Samarasinghe
 
Javascript basic course
Tran Khoa
 
Python Programming - VI. Classes and Objects
Ranel Padon
 
C++ programming introduction
sandeep54552
 
Variables in python
Jaya Kumari
 
Polymorphism
prabhat kumar
 
OOP interview questions & answers.
Questpond
 
(4) collections algorithms
Nico Ludwig
 
Paca oops slid
pacatarpit
 
Basic Javascript
Bunlong Van
 
Inheritance
Pranali Chaudhari
 
C++ Object oriented concepts & programming
nirajmandaliya
 
Chapter2 array of objects
Mahmoud Alfarra
 
Java basic concept
University of Potsdam
 
Constructor and Destructors in C++
sandeep54552
 
Advanced JavaScript
Fu Cheng
 
L2 datatypes and variables
teach4uin
 
Datatype in JavaScript
Rajat Saxena
 
DIWE - Programming with JavaScript
Rasan Samarasinghe
 

Viewers also liked (7)

PDF
Comunicado
soypublica
 
PPT
Desarrollo Local
guestc68e84
 
PDF
Love for science or 'Academic Prostitution' - DFD2014 version
Lourdes Verdes-Montenegro
 
PPTX
Canube. Ciencia abierta en la nube. Tarea 3.2 biocomputación
Arantxa Muñoz Pérez del Pulgar
 
PPSX
Design for Hypermedia - Class 09 (2015B)
Bryan Chung
 
PDF
Open Science and Executable Papers
Jose Enrique Ruiz
 
PPT
Capital Social Y Redes
guestc68e84
 
Comunicado
soypublica
 
Desarrollo Local
guestc68e84
 
Love for science or 'Academic Prostitution' - DFD2014 version
Lourdes Verdes-Montenegro
 
Canube. Ciencia abierta en la nube. Tarea 3.2 biocomputación
Arantxa Muñoz Pérez del Pulgar
 
Design for Hypermedia - Class 09 (2015B)
Bryan Chung
 
Open Science and Executable Papers
Jose Enrique Ruiz
 
Capital Social Y Redes
guestc68e84
 
Ad

Similar to C,s&s (20)

PDF
Data Science decoded- author: Rohit Dubey
Rohit Dubey
 
PDF
Review of c_sharp2_features_part_iii
Nico Ludwig
 
PPTX
Spl to the Rescue - Zendcon 09
Elizabeth Smith
 
PPTX
Docketrun's Python Course for beginners.pptx
wafoxeg441
 
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
PPTX
Advance python
pulkit agrawal
 
PDF
singh singhsinghsinghsinghsinghsinghsinghsinghsingh.pdf
horiamommand
 
KEY
Programming with Python - Week 3
Ahmet Bulut
 
PDF
C Programming - Refresher - Part II
Emertxe Information Technologies Pvt Ltd
 
ODP
Method Handles in Java
hendersk
 
PDF
python interview prep question , 52 questions
gokul174578
 
PDF
Python master class 2
Chathuranga Bandara
 
PDF
Python iteration
dietbuddha
 
PDF
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic Ukraine
 
PDF
🐍⚡ “Python Panache: Code Like a Pro, Not a Programmer!”
yashikanigam1
 
PPTX
Functions
Gaurav Subham
 
PPTX
Python-Magic-Methodsndndnndndndndndnnddnndndndndndn
2kawserahmed7
 
PPTX
Iterator - a powerful but underappreciated design pattern
Nitin Bhide
 
PDF
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
Nina Zakharenko
 
PDF
slidesgo-unlocking-the-power-of-python-an-in-depth-exploration-of-magic-metho...
2kawserahmed7
 
Data Science decoded- author: Rohit Dubey
Rohit Dubey
 
Review of c_sharp2_features_part_iii
Nico Ludwig
 
Spl to the Rescue - Zendcon 09
Elizabeth Smith
 
Docketrun's Python Course for beginners.pptx
wafoxeg441
 
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
Advance python
pulkit agrawal
 
singh singhsinghsinghsinghsinghsinghsinghsinghsingh.pdf
horiamommand
 
Programming with Python - Week 3
Ahmet Bulut
 
C Programming - Refresher - Part II
Emertxe Information Technologies Pvt Ltd
 
Method Handles in Java
hendersk
 
python interview prep question , 52 questions
gokul174578
 
Python master class 2
Chathuranga Bandara
 
Python iteration
dietbuddha
 
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic Ukraine
 
🐍⚡ “Python Panache: Code Like a Pro, Not a Programmer!”
yashikanigam1
 
Functions
Gaurav Subham
 
Python-Magic-Methodsndndnndndndndndnnddnndndndndndn
2kawserahmed7
 
Iterator - a powerful but underappreciated design pattern
Nitin Bhide
 
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
Nina Zakharenko
 
slidesgo-unlocking-the-power-of-python-an-in-depth-exploration-of-magic-metho...
2kawserahmed7
 
Ad

C,s&s