“Pobieranie ściągawek generatora Python” Kod odpowiedzi

Pobieranie ściągawek generatora Python

>>> permutations('abc', 2)                   #   a  b  c
[('a', 'b'), ('a', 'c'),                     # a .  x  x
 ('b', 'a'), ('b', 'c'),                     # b x  .  x
 ('c', 'a'), ('c', 'b')]                     # c x  x  .
Important Ibis

Pobieranie ściągawek generatora Python

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age  = age

class Employee(Person):
    def __init__(self, name, age, staff_num):
        super().__init__(name, age)
        self.staff_num = staff_num
Important Ibis

Pobieranie ściągawek generatora Python

from functools import wraps

def debug(func):
    @wraps(func)
    def out(*args, **kwargs):
        print(func.__name__)
        return func(*args, **kwargs)
    return out

@debug
def add(x, y):
    return x + y
Important Ibis

Pobieranie ściągawek generatora Python

def get_multiplier(a):
    def out(b):
        return a * b
    return out
Important Ibis

Pobieranie ściągawek generatora Python

>>> combinations_with_replacement('abc', 2)  #   a  b  c
[('a', 'a'), ('a', 'b'), ('a', 'c'),         # a x  x  x
 ('b', 'b'), ('b', 'c'),                     # b .  x  x
 ('c', 'c')]                                 # c .  .  x
Important Ibis

Pobieranie ściągawek generatora Python

>>> counter = count(10, 2)
>>> next(counter), next(counter), next(counter)
(10, 12, 14)
Important Ibis

Pobieranie ściągawek generatora Python

class <name>:
    def __init__(self, a):
        self.a = a
    def __repr__(self):
        class_name = self.__class__.__name__
        return f'{class_name}({self.a!r})'
    def __str__(self):
        return str(self.a)

    @classmethod
    def get_class_name(cls):
        return cls.__name__
Important Ibis

Pobieranie ściągawek generatora Python

>>> multiply_by_3 = get_multiplier(3)
>>> multiply_by_3(10)
30
Important Ibis

Pobieranie ściągawek generatora Python

def count(start, step):
    while True:
        yield start
        start += step
Important Ibis

Pobieranie ściągawek generatora Python

from itertools import count, repeat, cycle, chain, islice
Important Ibis

Odpowiedzi podobne do “Pobieranie ściągawek generatora Python”

Pytania podobne do “Pobieranie ściągawek generatora Python”

Więcej pokrewnych odpowiedzi na “Pobieranie ściągawek generatora Python” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu