+ All Categories
Home > Documents > F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC...

F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC...

Date post: 17-Jun-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
33
F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh Semester 1 — 2018/19 0 No proprietary software has been used in producing these slides Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 1 / 30
Transcript
Page 1: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

F21SC Industrial Programming:Python Advanced Language Features

Hans-Wolfgang Loidl

School of Mathematical and Computer Sciences,Heriot-Watt University, Edinburgh

Semester 1 — 2018/19

0No proprietary software has been used in producing these slidesHans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 1 / 30

Page 2: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Overloading

Operators such as +, <= and functions such as abs, str andrepr can be defined for your own types and classes.

Exampleclass Vector(object):# constructordef __init__(self, coord):self.coord = coord

# turns the object into stringdef __str__(self):return str(self.coord)

v1 = Vector([1,2,3])# performs conversion to string as aboveprint (v1)

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 2 / 30

Page 3: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Overloading

Exampleclass Vector(object):# constructordef __init__(self, coord):self.coord = coord

# turns the object into string: use <> as brackets, and ; as separatordef __str__(self):s = "<"if len(self.coord)==0:

return s+">"else:

s = s+str(self.coord[0])for x in self.coord[1:]:

s = s+";"+str(x);return s+">"

v1 = Vector([1,2,3]); print (v1)

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 3 / 30

Page 4: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Overloading arithmetic operations

Exampleimport math # sqrtimport operator # operators as functions

class Vector(object):...def __abs__(self):’’’Vector length (Euclidean norm).’’’return math.sqrt(sum(x*x for x in self.coord))

def __add__(self, other):’’’Vector addition.’’’return map(operator.add, self.coord, other.coord)

print(abs(v1))print(v1 + v1)

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 4 / 30

Page 5: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Overloading of non-symmetric operations

Scalar multiplication for vectors can be written either v1 * 5 or5 * v1.

Exampleclass Vector(object):...def __mul__(self, scalar):’Multiplication with a scalar from the right.’return map(lambda x: x*scalar, self.coord)

def __rmul__(self, scalar):’Multiplication with a scalar from the left.’return map(lambda x: scalar*x, self.coord)

v1 * 5 calls v1.__mul(5).5 * v1 calls v1.__rmul(5).

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 5 / 30

Page 6: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Overloading of indexing

Indexing and segment-notation can be overloaded as well:

Exampleclass Vector(object):

def __getitem__(self, index):’’’Return the coordinate with number index.’’’return self.coord[index]

def __getslice__(self, left, right):’’’Return a subvector.’’’return Vector(self.coord[left:right])

print v1[2]print v1[0:2]

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 6 / 30

Page 7: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Exercise (optional)

Define a class Matrix and overload the operations + und * toperform addition and multiplication on matrices.Define further operations on matrices, such as m.transpose(),str(m), repr(m).

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 7 / 30

Page 8: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Types

type(v) yields the type of v.Type-membership can be tested like thisisinstance(val,typ). E.g.>>> isinstance(5, float)False>>> isinstance(5., float)True

This check observes type-membership in the parent class. E.g.>>> isinstance(NameError(), Exception)True

issubclass checks the class-hierarchy.>>> issubclass(NameError, Exception)True>>> issubclass(int, object)True

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 8 / 30

Page 9: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Manual Class Generation

type(name,superclasses,attributes) creates a classobject with name name, parent classes superclasses, andattributes attributes.C = type(’C’,(),{}) corresponds to class C: pass.Methods can be passed as attributes:

Exampledef f (self, coord):self.coord = coord

Vec = type(’Vec, (object,), {’__init__’ : f})

Manual class generation is useful for meta-programming, i.e.programs that generate other programs.

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 9 / 30

Page 10: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Properties

Properties are attributes for which read, write and deleteoperations are defined.Construction:property(fget=None, fset=None, fdel=None, doc=None)

Exampleclass Rectangle(object):def __init__(self, width, height):self.width = widthself.height = height

# this generates a read only propertyarea = property(lambda self: self.width * self.height, # anonymous function, computing the areadoc="Rectangle area (read only).")

print("Area of a 5x2 rectange: ", Rectangle(5,2).area)

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 10 / 30

Page 11: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Controlling Attribute Access

Access to an attribute can be completely re-defined.This can be achieved as follows:__getattribute__(self, attr)__setattr__(self, attr, value)__delattr__(self, attr)

Example: Lists without append

Exampleclass listNoAppend(list):def __getattribute__(self, name):if name == ’append’: raise AttributeErrorreturn list.__getattribute__(self, name)

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 11 / 30

Page 12: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Static Methods

A class can define methods, that don’t use the current instance(self).

I Class methods can access class attributes, as usual.I Static methods can’t do that!.

Exampleclass Static:# static methoddef __bla(): print ("Hello, world!")hello = staticmethod(__bla)

The static method hello can be called like this:Static.hello()Static().hello()

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 12 / 30

Page 13: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Class/Instance Methods

A class or instance method takes as first argument a reference toan instance of this class.

Exampleclass Static:val = 5# class methoddef sqr(c): return c.val * c.valsqr = classmethod(sqr)

Static.sqr()Static().sqr()

It is common practice to overwrite the original definition of themethod, in this case sqr.Question: What happens if we omit the line with classmethodabove?

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 13 / 30

Page 14: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Function Decoration

The patterndef f(args): ...f = modifier(f)

has the following special syntax:@modifierdef f(args): ...

We can rewrite the previous example to:

Exampleclass Static:val = 5# class method@classmethoddef sqr(c): return c.val * c.val

More examples of using modifiers: Memoisation, Type-checking.Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 14 / 30

Page 15: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Memoisation with Function Decorators

We want a version of Fibonacci (below), that remembers previousresults (“memoisation”).

Exampledef fib(n):

"""Compute Fibonacci number of @n@."""if n==0 or n==1:

return 1else:

return fib(n-1)+fib(n-2)

NB: This version performs an exponential number of functioncalls!

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 15 / 30

Page 16: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Memoisation with Function Decorators

To visualise the function calls, we define a decorator for tracing:

Exampledef trace(f):"""Perform tracing on function @func@."""

def trace_func(n):print("++ computing", f.__name__," with ", str(n))return f(n)

return trace_func

and we attach this decorator to our fib function:

Example@tracedef fib(n): ....

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 16 / 30

Page 17: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Memoisation with Function Decorators

Now, we implement memoisation as a decorator.Idea:

I Whenever we call fib, we remember input and output.I Before calling a fib, we check whether we already have an output.I We use a dictionary memo_dict, to store these values.

This way, we never compute a Fibonacci value twice, and runtimebecomes linear, rather than exponential!

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 17 / 30

Page 18: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Memoisation with Function Decorators

Here is the implementation of the decorator:

Exampledef memoise(f):"""Perform memoisation on function @func@."""def memo_func(n, memo_dict=dict()):if n in memo_dict.keys():return memo_dict[n]

else:print("++ computing", f.__name__," with ", str(n))x = f(n)memo_dict[n] = xprint(".. keys in memo_dict: ", str(memo_dict.keys()));return x

return memo_func

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 18 / 30

Page 19: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Memoisation with Function Decorators

We attach this decorator to the fib function like this:

Example@memoisedef fib(n): ...

Nothing else in the code changes!See online sample memofib.py

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 19 / 30

Page 20: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Interpretation

Strings can be evaluated using the function eval, whichevaluates string arguments as Python expressions.>>> x = 5>>> eval ("x")5>>> f = lambda x: eval("x * x")>>> f(4)16

The command exec executes its string argument:>>> exec("print(x+1)")5

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 20 / 30

Page 21: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

CompilationThis performs compilation of strings to byte-code:>>> c = compile("map(lambda x:x*2,range(10))", # code

’pseudo-file.py’, # filename for error msg’eval’) # or ’exec’ (module) or ’single’ (stm)

>>> eval(c)<map object at 0x7f2e990e3d30>>>> for i in eval(c): print(i)0 ...Beware of indentation in the string that you are composing!>>> c2 = compile(’’’... def bla(x):... print x*x... return x... bla(5)... ’’’, ’pseudo’, ’exec’)>>> exec c225

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 21 / 30

Page 22: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

New features in Python 3.5

Python 3.5 brings several new features, especially:Coroutines with async and await syntaxA dedicated infix operator for matrix multiplicationType HintsAdditional Unpacking Generalizations% formatting support for bytes and bytearrayPre-3.5 but important:

I Several built-in functions now return iterators, rather than lists, e.gI dict.keys() and dict.values()I builtin higher-order functions such as map, filter, reduceI operators such as rangeI if in doubt, try it in the python shell, e.g.

Example>>> map(lambda x: x**2 , range(0,10))<map object at 0x7f8a87c17978>

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 22 / 30

Page 23: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Coroutines with async and await syntax

async and await are supported, as in C#:import asyncioasync def http_get(domain):

reader, writer = await asyncio.open_connection(domain, 80)writer.write(b’\r\n’.join([

b’GET / HTTP/1.1’,b’Host: %b’ % domain.encode(’latin-1’),b’Connection: close’,b’’, b’’

]))async for line in reader:

print(’>>>’, line)writer.close()

loop = asyncio.get_event_loop()try:

loop.run_until_complete(http_get(’example.com’))finally:

loop.close()Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 23 / 30

Page 24: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

A dedicated infix operator for matrix multiplication

You can use the @ operator for infix matrix mutiplication:

Exampleres = m1 @ m2

NumPy 1.10 supports this syntax as well:

Exampleones = np.ones(3)# builds:# array([ 1., 1., 1.])

m = np.eye(3)# builds the unit matrix

res = ones @ mprint(res)# builds: array([ 1., 1., 1.])

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 24 / 30

Page 25: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Type Hints

Type information can be added as hints to function arguments andreturn values.The semantics of these annotations is undefined.You can’t rely on types being checked statically!The type Any stands for an unknown type.Example:

Exampledef greeting(name: str) -> str:

return ’Hello ’ + name

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 25 / 30

Page 26: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

New features in Python 3.6

Python 3.6 brings several new features, especially:asynchronous generatorsasynchronous comprehensionssyntax for variable annotationsformatted string literals

0See https://docs.python.org/3/whatsnew/3.6.htmlHans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 26 / 30

Page 27: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Asynchronous generators

Python 3.6 adds support for native coroutines and async / awaitsyntax to Python 3.5This removes a Python 3.5 limitation: not possible to use awaitand yield in the same function body;

Exampleasync def ticker(delay, to):

"""Yield numbers from 0 to *to* every *delay* seconds."""for i in range(to):

yield iawait asyncio.sleep(delay)

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 27 / 30

Page 28: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Asynchronous generators

Python 3.6 adds support for native coroutines and async / awaitsyntax to Python 3.5This removes a Python 3.5 limitation: not possible to use awaitand yield in the same function body;

Exampleasync def ticker(delay, to):

"""Yield numbers from 0 to *to* every *delay* seconds."""for i in range(to):

yield iawait asyncio.sleep(delay)

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 27 / 30

Page 29: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Asynchronous comprehensions

Python 3.6 adds support for using async for in list, set, dictcomprehensions and generator expressions

Exampleresult = [i async for i in aiter() if i % 2]

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 28 / 30

Page 30: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Asynchronous comprehensions

Python 3.6 adds support for using async for in list, set, dictcomprehensions and generator expressions

Exampleresult = [i async for i in aiter() if i % 2]

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 28 / 30

Page 31: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Syntax for variable annotations

Exampleprimes: List[int] = []

captain: str # Note: no initial value!

class Starship:stats: Dict[str, int] = {}

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 29 / 30

Page 32: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Formatted string literals

formatted string literals are similar to the format strings acceptedby str.format();formatted string literals are prefixed with ’f’ and are similar to theformat strings accepted by str.format().they contain replacement fields surrounded by curly braces

Example>>> name = "Fred">>> f"He said his name is {name}."’He said his name is Fred.’>>> width = 10>>> precision = 4>>> value = decimal.Decimal("12.34567")>>> f"result: {value:{width}.{precision}}" # nested fields’result: 12.35’

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 30 / 30

Page 33: F21SC Industrial Programming: Python Advanced Language ...hwloidl/Courses/F21SC/slides... · F21SC Industrial Programming: Python Advanced Language Features Hans-Wolfgang Loidl School

Formatted string literals

formatted string literals are similar to the format strings acceptedby str.format();formatted string literals are prefixed with ’f’ and are similar to theformat strings accepted by str.format().they contain replacement fields surrounded by curly braces

Example>>> name = "Fred">>> f"He said his name is {name}."’He said his name is Fred.’>>> width = 10>>> precision = 4>>> value = decimal.Decimal("12.34567")>>> f"result: {value:{width}.{precision}}" # nested fields’result: 12.35’

Hans-Wolfgang Loidl (Heriot-Watt Univ) Python Advanced F20SC/F21SC — 2018/19 30 / 30


Recommended