语言相关问题 2¶
目录
- 语言相关问题 2
- Monkey Patching
- How are Dict and Set implemented internally?
- How are list implemented in CPython?
- What is MRO in Python? How does it work?
- What are descriptors?
- How memory is managed in Python?
- Why isn’t all memory freed when CPython exits?
- Why are there separate tuple and list data types?
- What is output?
- What is the difference between Python 2 and 3?
- What are metaclasses in Python
- Write
timeit
decorator for measure time of function execution. - Make a prime number list from (1,100)
- What do these mean to you:
@classmethod
,@staticmethod
,@property
? - Reference
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?¶
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.
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 _