meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:python [2017/08/02 14:07]
niziak
programming:python [2020/07/03 09:51] (current)
niziak ↷ Page moved from python to programming:python
Line 1: Line 1:
 +====== Python ======
 +
 +====== OOP ======
 +
 +===== Call parent constuctor ======
 +
 +<code python>​super().__init__()</​code>​
 +
 +====== String formatting ======
 +
 +  * using the % operator (old)
 +<code python>
 +logger.debug('​Got argument %s.' % arg)
 +</​code>​
 +
 +  * str.format (new: string formatters)
 +<code python>
 +print("​Sammy has {} balloons."​.format(5))
 +print("​Sammy has {} and {} balloons."​.format(5,​ 7))
 +print("​Sammy has {0} and {1} balloons."​.format(5,​ 7))
 +print("​Sammy has {1} and {0} balloons."​.format(5,​ 7))
 +</​code>​
 +
 +  * interpolated strings
 +<code python>
 +print("​{firstname} {lastname}"​.format(firstname="​Horst",​ lastname="​Gutmann"​))
 +</​code>​
 +
 +  * new f-strings in Python 3.6
 +<code python>
 +name = '​Fred'​
 +age = 42
 +print(f'​He said his name is {name} and he is {age} years old.')
 +</​code>​
 +
 +===== logger =====
 +
 +Logger is optimized to defer avaluation of arguments until it is needed, so for best performance use ''​%''​ formating.
 +Instead of this:
 +<code python>​logger.error('​oops caused by %s' % exc)</​code>​
 +do that:
 +<code python>​logger.error('​oops caused by %s', exc)</​code>​
 +
 +
 ====== Exceptions ====== ====== Exceptions ======
   * [[https://​docs.python.org/​3/​library/​exceptions.html#​exception-hierarchy|exception-hierarchy]]   * [[https://​docs.python.org/​3/​library/​exceptions.html#​exception-hierarchy|exception-hierarchy]]
Line 6: Line 50:
  
 <code python> <code python>
-except:+try: 
 +    do_something() 
 +except ​Exception:
     pass     pass
 </​code>​ </​code>​