bbcqx

Python hints before interview

Aug 2nd, 2021 (edited)
630
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Naming, Encapsulation
  2. import operator
  3. from functools import reduce
  4.  
  5.  
  6. class AnyClass:
  7.     def __init__(self):
  8.         self.a = 1
  9.         self._a = 2
  10.         self.__a = 3
  11.         self.__a__ = 4
  12.  
  13.  
  14. any_object = AnyClass()
  15.  
  16. print(any_object.a)  # OK
  17. print(any_object._a)  # OK
  18. print(any_object._AnyClass__a) # OK
  19. print(any_object.__a__)  # OK
  20.  
  21.  
  22. # Iterators
  23.  
  24. class CustomIterator():
  25.     def __iter__(self):
  26.         self.pointer = 0
  27.         return self
  28.  
  29.     def __next__(self):
  30.         self.pointer += 1
  31.         if self.pointer == 10:
  32.             raise StopIteration
  33.         return self.pointer
  34.  
  35.  
  36. custom_iterator = CustomIterator()
  37.  
  38. for i in custom_iterator:
  39.     print(i)
  40.  
  41.  
  42. # Generators
  43.  
  44. def new_number():
  45.     i = 0
  46.     while True:
  47.         i += 1
  48.         yield i
  49.         # return i
  50.  
  51.  
  52. a = new_number()
  53. print(a)  # <generator object new_number at 0x102f52660>
  54. print(next(a))  # 1
  55. print(next(a))  # StopIteration if `return i` present else 2
  56. print(next(a))  # 3
  57.  
  58.  
  59. # Decorators
  60.  
  61. def decorator(func):
  62.     def inner(a, b):
  63.         print('decorator in')
  64.         result = func(a, b)
  65.         print('decorator out')
  66.         return result
  67.  
  68.     return inner
  69.  
  70.  
  71. def some_function(a, b):
  72.     print(a + b)
  73.  
  74.  
  75. # some_function = decorator(some_function)
  76. # some_function(1, 2)
  77.  
  78.  
  79. # equals to
  80. # @decorator
  81. # def some_function():
  82. #     print('Yooo I\'m a function')
  83.  
  84.  
  85. def outer_decorator(arg1, arg2):
  86.     def decorator(func):
  87.         def sub_inner(a, b):
  88.             for i in range(arg1):
  89.                 print('decorator in')
  90.             result = func(a, b)
  91.             for i in range(arg2):
  92.                 print('decorator out')
  93.             return result
  94.  
  95.         return sub_inner
  96.  
  97.     return decorator
  98.  
  99.  
  100. def summ(a, b):
  101.     print(a + b)
  102.  
  103.  
  104. # @outer_decorator(2, 2)
  105. # def summ(a, b):
  106. #     print(a, b)
  107. # equals to
  108.  
  109. summ = outer_decorator(2, 2)(summ)(5, 5)
  110.  
  111.  
  112. # Context managers (with)
  113.  
  114. class ContextResolver:
  115.     def __enter__(self):
  116.         print('In')
  117.         return 1
  118.  
  119.     def __exit__(self, exc_type, exc_val, exc_tb):
  120.         print('Out')
  121.  
  122.  
  123. cr = ContextResolver()
  124.  
  125. with cr as a:
  126.     print(a)
  127.  
  128. # In
  129. # 1
  130. # Out
  131.  
  132. # Scopes (global, local, enclosed)
  133.  
  134. some_var = 10
  135.  
  136.  
  137. def func():
  138.     global some_var  # global scope
  139.     some_var += 1
  140.     return some_var
  141.  
  142.  
  143. print(func())
  144.  
  145.  
  146. def outer_function():
  147.     some_var = 5
  148.  
  149.     def inner_function():
  150.         global some_var  # nonlocal some_var
  151.         some_var += 1
  152.         return some_var
  153.  
  154.     return inner_function()
  155.  
  156.  
  157. print(outer_function())
  158.  
  159.  
  160. # Map, reduce, filter
  161.  
  162. def some_func(arg):
  163.     return str(arg) + ' lol it was made in map'
  164.  
  165.  
  166. print(list(map(some_func, (1, 2, 3)))) # ['1 lol it was made in map', '2 lol it was made in map', '3 lol it was made in map']
  167.  
  168.  
  169. def some_definer(arg):
  170.     if isinstance(arg, str):
  171.         return arg  # if true it its not getting filtered
  172.  
  173.  
  174. print(list(filter(some_definer, (1, 2, 3, 'Max', 'Josh')))) # ['Max', 'Josh']
  175.  
  176. print(reduce(operator.add, [1, 2, 3, 4, 5])) # 15
  177.  
RAW Paste Data