Blender coursesPython coursesEnglish from courses


Duck typing-gal azt mondjuk, hogy futasidoben ez a fuggveny ilyen es olyan metodusokkal es/vagy propertykkel kell rendelkezzen.


Design patterns

Instead of doing this:

class User(DbObject):

We can do something like this:

class User:
    _persist_methods = ['get', 'save', 'delete']

    def __init__(self, persister):
        self._persister = persister

    def __getattr__(self, attribute):
        if attribute in self._persist_methods:
            return getattr(self._persister, attribute)


List, dictionary comprehensions – great article

flipped = {value: key for key, value in original.items()

youtube, list comprehension videos:


class static variables, members

Static method vs Class method

Decorators tutorial

Concatenated string:
 s = ("this is a very"
      "long string too"
      "for sure ..."

result: 'this is a verylong string toofor sure ...'

Modules/packages, stackoverflow

Any Python file is a module, its name being the file’s base name without the .py extension. A package is a collection of Python modules: while a module is a single Python file, a package is a directory of Python modules containing an additional file, to distinguish a package from a directory that just happens to contain a bunch of Python scripts. Packages can be nested to any depth, provided that the corresponding directories contain their own file.

flipped_dict_value_key_pairs = {y: x for x, y in {‘apple’: 2, ‘orange’: 1, ‘pear’: 0}.iteritems()}


Decorator examples

def decorator(old_func):
    def new_func(*args, **kwds):
        print "Before"
        old_func(*args, **kwds)
        print "After"
    return new_func

# with this you pack your function into a wrapper
def summa(a,b):
    print a + b

def jo_reggelt():
    print "jo reggelt"



a == b  compares the values of objects
a is b    compares their identities.

find same elements in needles and haystack:

found = len(set(needles) & set(haystack))
found = len(set(needles).intersection(haystack))

a == b == c == d

a = dict(one=1, two=2, three=3)
b = {‘one’: 1, ‘two’: 2, ‘three’: 3}
c = dict(zip([‘one’, ‘two’, ‘three’], [1, 2, 3]))
d = dict([(‘two’, 2), (‘one’, 1), (‘three’, 3)])
e = dict({‘three’: 3, ‘one’: 1, ‘two’: 2})
a == b == c == d == e


>>> s = “paros” if n %2 == 1 else “paratlan”

>>> n=”a”
>>> x= 1 if len(n) > 8 else “hosszu” if len(n) > 6 else “rovid”

>>> def plus(n):
… return n+1
plus(2 if len(nev)>3 else 6)

py2-ben a list comprehension erteke szivarog:

>>> x = ‘my precious’
>>> dummy = [x for x in ‘ABC’]
>>> x

inner function article

def outer():
    def inner():
        inner.y += 1
        return inner.y
    inner.y = 0
    return inner

f = outer()
g = outer()
print(f(), f(), g(), f(), g()) #prints (1, 2, 1, 3, 2)

print(‘Before‘, a)
a[2:7] = [99, 22, 14]   # replace the selected part with new elements
print(‘After’, a)
Before [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]
After [‘a’, ‘b’, 99, 22, 14, ‘h’]


matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
squared = [[x**2 for x in row] for row in matrix]

with open(‘/tmp/random.bin’, ‘wb’) as f:

seq = range(8)

[[x*y for y in seq] for x in seq]


elements = [“apple”, “dog”, “bird”]




if sys.version_info < (2, 7):


Doctest – really good article

Module showing how doctests can be included with source code
Each ‘>>>‘ line is run as if in a python shell, and counts as a test.
The next line, if not ‘>>>‘ is the expected output of the previous line.
If anything doesn’t match exactly (including trailing spaces), the test fails.
def multiply(a, b):
    >>> multiply(4, 3)
    >>> multiply('a', 3)
    return a * b

python m doctest v <file>



  • raise without params, Using raise with no arguments re-raises the last exception: