This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample(): | |
def __init__(self): | |
self.a = 1 | |
self._b = 2 | |
self.__c = 3 | |
>>> t = Sample() | |
>>> dir(t) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> def function(class): | |
File "<stdin>", line 1 | |
def function(class): | |
^ | |
SyntaxError: invalid syntax | |
>>> def function(class_): | |
... pass | |
... | |
>>> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import example_module | |
>>> example_module.external_func() | |
23 | |
>>> example_module._internal_func() | |
42 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is example_module.py: | |
def external_func(): | |
return 23 | |
def _internal_func(): | |
return 42 | |
>>> from example_module import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Example: | |
def __init__(self): | |
self.foo = 21 | |
self._bar = 25 | |
>>> t = Example() | |
>>> t.foo | |
21 | |
>>> t._bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nondeterministic_function(a, b): | |
if a % b == 0: | |
return "Even number" | |
return "Odd number" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def deterministic_function(a, b): | |
return a + b | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from functools import lru_cache | |
@lru_cache(maxsize=256) | |
def fibonacci_lru_cache(n: int): | |
if n == 0: | |
return 0 | |
elif n == 1: | |
return 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from functools import wraps | |
def memoize(func): | |
cache = dict() | |
@wraps(func) | |
def to_cache(*args): | |
if args in cache: | |
return cache[args] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci(n: int): | |
""" Define function fibonacci with recursion""" | |
if n == 0: | |
return 0 | |
elif n == 1: | |
return 1 | |
return fibonacci(n-1) + fibonacci(n-2) | |
N = 35 | |
fib = fibonacci(N) |