Flow control
●
if / elif / else
●
while
●
try / except / finally
●
raise
●
with
●
for
●
yield
●
pass
Slide 16
Slide 16 text
For in Python
●
No C-style for
●
Python only uses iterations on collections:
– for i in range(10):
print(i)
Slide 17
Slide 17 text
List comprehension
●
topStudents = [
student
for student in students
if student.average >= 85
]
●
coordinates = [
(x + 5, y - 2)
for x in range(1, 11)
for y in range(1, 6)
]
coordinates = [ ]
for x in range(1, 11):
for y in range(1, 6):
coordinates.append(
(x + 5, y – 2)
)
The WITH statement
with open(“readme.txt”, “r”) as myFile:
for line in myFile:
#Process line here
print(line)
Slide 20
Slide 20 text
Functions
●
def f(x, y):
return x + y
●
A new function object is created and the
name “f” gets bound to it.
Slide 21
Slide 21 text
Functions as objects
●
def f(x, y):
return x + y
●
h = f
●
h(90, 100)
Slide 22
Slide 22 text
No overloading in Python
●
In Python, def creates a function object
and binds a name to it!
●
Cannot do overloading!!! X___X!
Slide 23
Slide 23 text
Parameters VS Argument
●
Parameter = variable in a function
signature. It's a placeholder for the actual
value
●
Argument = actual value passed when
invoking the function
Slide 24
Slide 24 text
Function parameters
●
Standard parameters: f(x, y)
●
Default values: f(x, y, z = 90)
●
Arbitrary sequence: f(*args)
●
Keyword params: f(**kwargs)
●
They can all be introduced, in this order:
f(x, y, z = 90, *args, **kwargs)
Slide 25
Slide 25 text
Function arguments
●
Given a function f(x, y)...
●
...pass by position: f(9, 10)
●
...pass by name: f(y = 10, x = 9)
●
...unpack a list of arguments:
– myParamsSequence = [9, 10]
– f(*myParamsSequence)
●
...pass a map of arguments:
– myParamsMap = { “x”: 9, “y”: 10” }
– f(**myParamsMap)
Slide 26
Slide 26 text
Lambdas
●
myFunction = lambda x, y: x + y
●
myFunction(90, 100)
Slide 27
Slide 27 text
Classes
●
class Panda(object):
pass #can be used for classes, too
●
In Python 3, you can just write:
class Panda:
pass
Slide 28
Slide 28 text
Creating methods
●
class Panda(object):
def sayHello(self):
print(“Hello! ^__^!”)
●
self is the first parameter of instance
methods, and it's passed as an implicit
argument! It refers to the current instance,
just like this in Java/C#.
Slide 29
Slide 29 text
No overloading for methods
●
Methods are bound functions
●
Methods do not support overloading
Slide 30
Slide 30 text
Fields and constructor
●
class Panda(object):
def __init__(self, name):
self._name = name
self._picnicChests = 0
def addPicnicChest(self, picnicChest):
self._picnicChests += 1
def getName(self):
return self._name
Slide 31
Slide 31 text
No access control in Python
If a member of an object should not be
used by other objects, just prepend “_” to
its name.
For example:
●
self._privateField = 0
●
def _privateMethod(self):
pass
Object's name resolution
3. Super namespaces
2. Class namespace
1. Object namespace
… = obj.name
Slide 36
Slide 36 text
Multiple inheritance
●
Python does not define explicit interfaces...
●
...but it supports multiple inheritance!
●
class Panda(Bear, VeryCuteAnimal):
pass
Slide 37
Slide 37 text
Diamond resolution
Just too difficult to learn! ^__^''''
Slide 38
Slide 38 text
Explicit method resolution
●
Given an object instance, how to call the
method of a specific superclass?
– Super1.f(obj, )
– Super2.f(obj, )
Slide 39
Slide 39 text
Overriding methods
●
Python supports overriding!
●
But how to call the version provided by the
superclass?
Slide 40
Slide 40 text
super()
●
super(CurrentClassName, self).f()
●
In Python 3:
super().f()
●
Not mandatory (you can use explicit
method resolution instead)
Slide 41
Slide 41 text
Static methods
●
class Panda(object):
@staticmethod
def myStaticMethod(x, y):
return x+y
●
Static methods can be called via the class
or via any instance
Slide 42
Slide 42 text
Class methods
●
class Bear(object):
@classmethod
def f(cls, x, y):
return x + y
●
They can be called via class or instance
Slide 43
Slide 43 text
Operator overloading
●
To overload “+”
– def __add__(self, other):
return ...
●
There are several operators available
Equality and hashcode
●
__eq__(self, other) and __neq__(self,
other). Both should be implemented.
●
__hash__(self) returns the hash code
Slide 47
Slide 47 text
Comparisons
●
Binary operators: __gt__, __gte__, __lt__,
__lte__
●
Alternatively, __cmp__ behaves just like
Java's compareTo()
Slide 48
Slide 48 text
Modules
●
Python files are modules...
●
...but not all modules are files! ^__^!
●
PYTHONPATH
Slide 49
Slide 49 text
Importing a module
●
import re
●
from re import compile
●
from re import * (good just while learning)
Slide 50
Slide 50 text
Module caching and reloading
●
Importing a module executes it only the first
time
●
The module cache is sys.modules
●
reload() reloads a module
●
.pyc files to optimize loading in future
sessions
Slide 51
Slide 51 text
Packages
●
Python supports packages!
●
Directory structure
●
__init__.py
●
Within a package, use relative import!
Slide 52
Slide 52 text
Further execution options
●
Py2exe for Windows
●
PyInstaller for Windows & Unix
●
Jython
●
IronPython
●
Boo