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