https://docs.python.org/3/library/functions.html
https://docs.python.org/3/library/builtins.html
# Sprawdzenie listy obiektów wbudowanych (wyjątki i inne klasy, funkcje). import __builtin__ # Py2 dir(__builtin__) zip # funkcja wbudowana zip = "qwerty" # zmieniamy referencję zip __builtin__.zip # dostęp do oryginalnego zip __builtin__.zip = "qwerty" # HORROR! Py2
import builtins # Py3 builtins.zip # dostęp do oryginalnego zip
Nazwy typów: basestring(), bool(), complex(), dict(), float(), frozenset(), int(), list(), long() [Py2], set(), str(), tuple(), unicode() [Py2].
Funkcje matematyczne i narzędziowe: abs(), all(), any(), chr(), divmod(), hex(), len(), map(), max(), min(), next() [Py2.6+]. oct(), ord(), pow(), range(), repr(), reversed(), round(), sorted(), sum(), unichr(), xrange(), zip().
Programowanie obiektowe: classmethod(), delattr(), dir(), getattr(), hasattr(), isinstance(), issubclass(), object(), property(), setattr(), staticmethod(), super().
Inne funkcje: __import__(), bin() [Py2.6+], callable(), cmp() [Py2], compile(), enumerate(), eval(), execfile(), file(), filter(), format() [Py2.6+], globals(), hash(), help(), id(), input(), iter() [Py2.2+], locals(), open(), raw_input(), reduce() [Py2], reload(), slice(), type(), vars().
Wbudowane funkcje przydatne przy pracy na listach:
Funkcje map() i filter() można łatwo zastąpić listą składaną.
def cube(x):
return x * x * x
list(map(cube, range(10)))
[cube(x) for x in range(10)] # lista składana
L = [-2, -1, 0, 1, 2]
list(map(abs, L)) # [2, 1, 0, 1, 2]
[abs(x) for x in L] # lista składana
L = [[1, 2], [3, 4]] # lista list
list(map(sum, L)) # [3, 7]
[sum(x) for x in L] # lista składana
# Liczba argumentów funkcji musi zgadzać się z liczbą
# dostarczonych list, a listy muszą mieć równą długość.
def calc(x, y):
return 2 * x + y
L = range(9)
list(map(calc, L, L))
[calc(x, y) for (x, y) in zip(L, L)] # lista składana
def check(x):
return x % 2 != 0 and x % 3 != 0
list(filter(check, range(30)))
[x for x in range(30) if check(x)] # lista składana
def add(x, y):
return x + y
def add2(x, y): # tu widać zależność od startowej wartości
return 2 * x + y
# Składnia:
# reduce(function, sequence[, initial]) -> value
# reduce(add, [1, 2, 3, 4, 5]) oblicza ((((1+2)+3)+4)+5).
# reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) oblicza ((((1+2)+3)+4)+5).
# Opcjonalna wartość jest umieszczana przed listą argumentów
# i stanowi domyślną wartość na wypadek pustej listy
# (a może też listy z jednym elementem?).
# reduce(f, []) zwraca wyjątek TypeError.
# reduce(f, [], x0) zwraca x0.
# reduce(f, [x1]) zwraca x1, czyli nie korzysta z funkcji.
# reduce(f, [x1], x0) zwraca f(x0, x1).
# reduce(f, [x1, x2]) zwraca f(x1, x2).
# reduce(f, [x1, x2], x0) zwraca f(f(x0, x1), x2).
alist = range(11)
result1 = sum(alist)
result1 = reduce(add, alist, 0)
result1 = reduce(add, alist)
# Równoważny kod z pętlą (bez wartości początkowej).
if len(alist) == 0:
raise TypeError()
elif len(alist) == 1:
result2 = alist[0]
else:
result2 = add(alist[0], alist[1])
for item in alist[2:]:
result2 = add(result2, item)
assert result1 == result2 # porównanie wyników
def my_sum(sequence):
"""Slow sum()."""
return reduce(lambda x,y: x+y, sequence, 0)
# import math
# math.factorial(n)
def my_factorial(n):
"""Slow factorial()."""
return reduce(lambda x,y: x*y, range(1, n+1), 1)
def gcd(a, b):
"""Compute the greatest common divisor."""
while b:
a, b = b, a % b
return a
def lcm(a, b):
"""Compute the least common multiple."""
return a * b / gcd(a, b)
# Najmniejsza wspólna wielokrotność kilku liczb.
# Zastosowane w pakiecie SymPy do obliczania rzędu
# permutacji danej jako iloczym cykli.
reduce(lcm, sequence, 1)