python generator cheat sheet download

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__

4
4
Sina Amiri 75 points

                                    &gt;&gt;&gt; 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  .

4 (4 Votes)
0
4.43
7

                                    &gt;&gt;&gt; multiply_by_3 = get_multiplier(3)
&gt;&gt;&gt; multiply_by_3(10)
30

4.43 (7 Votes)
0
0
8
Pukku 100 points

                                    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

0
0
4.43
7
RedRiderX 130 points

                                    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

4.43 (7 Votes)
0
4.4
10
Chilemagic 115 points

                                    &gt;&gt;&gt; counter = count(10, 2)
&gt;&gt;&gt; next(counter), next(counter), next(counter)
(10, 12, 14)

4.4 (10 Votes)
0
4.17
6

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

4.17 (6 Votes)
0
3.67
3
Nickdmax 65 points

                                    from functools import wraps

def debug(print_result=False):
    def decorator(func):
        @wraps(func)
        def out(*args, **kwargs):
            result = func(*args, **kwargs)
            print(func.__name__, result if print_result else '')
            return result
        return out
    return decorator

@debug(print_result=True)
def add(x, y):
    return x + y

3.67 (3 Votes)
0
0
8
Jura 120 points

                                    &gt;&gt;&gt; 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

0
0
4.75
4

                                    @decorator_name
def function_that_gets_passed_to_decorator():
    ...

4.75 (4 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source