Skip to content

Instantly share code, notes, and snippets.

class Example:
class_attr_1: str = 'attr_1'
@classmethod
def foo(cls, message: str) -> None:
print(f"Message sent from foo: {message}")
# access class attribute
print(cls.class_attr_1)
class Example:
@classmethod
def foo(cls, message: str) -> None:
print(f"Message sent from foo: {message}")
@staticmethod
def bar(message: str) -> None:
print(f"Message sent from bar: {message}")
if __name__ == "__main__":
class Example:
@staticmethod
def bar(arg1, arg2, ...):
pass
class Example:
@classmethod
def foo(cls, arg1, arg2, ...):
pass
# DP
fib_seq = [0, 1]
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
if len(fib_seq) == n-1:
# recursion
def fib(n):
if n==0:
return 0
if n==1:
return 1
return fib(n-1) + fib(n-2)
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
fn_2 = 0
fn_1 = 1
for i in range(n-1):
@att288
att288 / connect_generators.py
Last active May 5, 2019 21:49
connect_generators.py
def my_gen_1(n):
for i in range(n):
yield f'g1: {i}'
def my_gen_2(n):
for j in range(n):
yield f'g2: {-j}'
def my_gen_master(n):
yield from my_gen_1(n)
@att288
att288 / generator_send.py
Last active May 5, 2019 21:19
generator_send.py
def multiplier(n):
step = None
result = 1
for i in range(1, n+1):
if step is None:
step = 1
result *= (i*step)
print(f"i:{i}, step={step}")
step = yield result
@att288
att288 / fibonacci_sequence_generator.py
Last active May 5, 2019 20:36
fibonacci_sequence_generator.py
def fibonacci_sequence(n):
curr_val = 0
next_val = 1
for i in range(n):
yield curr_val
curr_val, next_val = next_val, curr_val + next_val
if __name__ == '__main__':
fib_seq = []
for fn in fibonacci_sequence(10):