语言相关问题 2

Monkey Patching

  • What is monkey patching?
  • How to use in Python? Example?
  • Is it ever a good idea?

What are descriptors?

  • Is there a difference between a descriptor and a decorator?

How memory is managed in Python?

  • How manage memory

  • Python memory is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have an access to this private heap and interpreter takes care of this Python private heap.

  • The allocation of Python heap space for Python objects is done by Python memory manager. The core API gives access to some tools for the programmer to code.

  • Python also have an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.

    https://assets.interviewbit.com/assets/skill_interview_questions/python/python-memory-manager-06d44b2add8b64c72f76994b4c759096df11a75a5b32b611da3371b8c6f0c7e6.png.gz

What is output?

>>> -12 % 10
>>> -12 // 10

Write timeit decorator for measure time of function execution.

import time
from functools import wraps

def timeit(func):
    @wraps(func)
    def _(*args, **kwargs):
        s = time.perf_counter()
        r = func(*args, **kwargs)
        e = time.perf_counter()
        print(e-s)
        return r
    rerturn _