SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
1.
Functions in Python
arguments, lambdas, decorators, generators
2.
Defining simple functions:
>>> def my_func():
... print "Inside my function"
...
>>> my_func()
Inside my function
3.
Function with arguments - basics
>>> def func_with_args(a, b):
... print 'a: ', a
... print 'b: ', b
...
>>> func_with_args(5, 'c')
a: 5
b: c
4.
Two types of arguments
● positional - arguments order matters
>>> func_with_args(5, 'c')
a: 5
b: c
● keyword - argument name matters
func_with_args(b=5, a='c')
a: c
b: 5
5.
Mixing position and keyword
arguments
>>> def func_with_args(a, b, c):
... print a, b, c
...
>>> func_with_args(1, c=3, b=2)
123
>>> func_with_args(a=1, 3, b=2)
File "<stdin>", line 1
SyntaxError: non-keyword arg after keyword arg
16.
Python introspection
""" In computing, type introspection is the ability of a
program to examine the type or properties of an object
at runtime. """
Wikipedia - Type Introspection
17.
Introspection basics
>>> def func(a, b, c):
... """ Just an example how introspection works """
... pass
>>> help(func)
Help on function func in module __main__:
func(a, b, c)
Just an example how introspection works
18.
Methods related to introspection
● type(var) # show the type of the object
● dir(var) # lists object's methods and attributes
● id(var) # return object specific identificator
● getattr(obj, <attribute name>, <default value>)
● hasattr(obj, <attribute name>)
● globals()
● locals()
● callable(obj) # returns True if the object is callable
20.
Lambdas and print
# Python 2.7.3
>>> a = lambda x: print x # print is a statement
File "<stdin>", line 1
a = lambda x: print x
Lambdas can not contain statements
# Python 3.2.3
>>> f = lambda x: print(x) # print is a function
>>> f(5)
5
21.
Lambdas usage
>>> map(lambda x: x**2, range(5))
[0, 1, 4, 9, 16]
>>> [x**2 for x in range(5)]
[0, 1, 4, 9, 16]
>>> reduce(lambda a, x: a+x, range(5))
10
Avoid using lambdas and check Guido van Rossum's vision
for lambdas http://www.artima.com/weblogs/viewpost.jsp?thread=98196
22.
Generators
yield x # Generator function send protocol
>>> def a():
... yield 5
... yield 6
... yield 7
>>> a()
<generator object a at 0x158caa0>
>>> a()
<generator object a at 0x158caa0>
23.
Generators - range vs xrange
>>> range(10**10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError
>>> xrange(10**10)
xrange(10000000000)
# range has been replaced by xrange in Python 3.x
24.
Creating generators
>>> def fib():
... a, b = 0, 1
... while True:
... yield a, b
... a, b = b, a+b
...
>>> g = fib()
>>> g
<generator object fib at 0x19b4be0>
30.
Decorators simplified
>>> def decorator(func):
... print 'Entering decorator'
... return func
...
>>> @decorator
... def f():
... print 'Executing F'
# Using the decorator above is the same as using:
>>> f = decorator(f)
31.
Functions as Decorators
>>> def decorator(func):
... def wrap():
... print 'Calling function %s' % func.__name__
... func()
... print 'Exiting decorator'
... return wrap
...
>>> @decorator
... def f():
... print 'Executing F'
...
>>> f()
Calling function f
Executing F
Exiting decorator
32.
Functions as decorators - part II
>>> def decorator(func):
... def wrap(a, b, c):
... print 'Function f called with %s, %s, %s' % (a, b, c)
... return func(a, b, c)
... return wrap
...
>>> @decorator
... def f(a, b, c):
... return a + b + c
...
>>> f(1, 2, 3)
Function f called with 1, 2, 3
6
34.
Decorators with arguments
>>> def decorator(debug=False):
... def wrap(func):
... def f_wrap(a, b, c):
... if debug:
... print 'Function f called with %s, %s, %s' % (a,
b, c)
... return func(a, b, c)
... return f_wrap
... return wrap
35.
Decorators with arguments
>>> @decorator(debug=True)
... def f(a, b, c):
... return a + b + c
...
>>> f(1, 2, 3)
Function f called with 1, 2, 3
6
>>> @decorator(debug=False)
... def f(a, b, c):
... return a + b + c
...
>>> f(1, 2, 3)
6
37.
Problems to solve
1) Create a function that accept both positional and
keyword arguments and returns their sum
2) Create a generator that implements a dice that "brokes"
after the 10th roll
3) Create a decorator that converts letters to number
according to their position in the alphabet and combine it
with the solution of problem #1. When converting letters to
numbers make them lowercase and count from 0 i.e. a==0,
b==1.
38.
About Me
eng. Ilian Iliev
● Web Developer for 9+ years
● Python/Django fan for 3+ years
● ilian@i-n-i.org
● http://ilian.i-n-i.org
● https://github.com/IlianIliev/