python study notes[4]
文章目录
- reference counting
- CFFI
- function
- Arbitrary Argument Lists
- references
reference counting
- reference counting is by no means part of the garbage collecting stratege ,the objects will be not released although they do not serve for accessing.
- the garbage collecting facilities of CPython has a few flaws which reference-counting is too strong restriction.for example, if you open a lot of files and forget to close them,then the CPython’s GC cycle inspect them repeatedly.so many opend files which is by no means used lead to not only the waste of memory but also reduction of GC’s efficiency. In this situation, weak references may is the best solution.
- the weakref survives except for few event such as reference cycles while the
__del__()
function runs.the del of object can be only called once in PyPy, but CPython perhaps call it more than once bacause that the object can resurrect and die again,by the way,the newer CPythons call destructors one time only.
CFFI
- CFFI is a recommended way which help python programmer to call C code with C libraries.CFFI is available for both CPython and PyPy,the PyPy’s JIT also support it.
- CFFI contribute to call C code from Python without a 3rd language.
function
- the arguments passed into function may be both poistion and keyword.
for example:
def fun1(x=1,y,z):return x+y+z
fun1(11,22,z=6)
fun1(y=33,z=22)
/
and *
can be used in parameters of function for indicating that positional-only, positional-or-keyword, and keyword-only.that is as follows.
def fun(a, b, /, x, *, y1,y2):----------- ---------- ----------| | || Positional or keyword || - Keyword only-- Positional only
for example:
def fun1(x,y,/,z1,z2,*,a,b):print(x+y+z1+z2+a+b)
fun1(11,22,z2=33,z1=99,a=4,b=9)
fun1(11,22,33,z2=99,a=4,b=9)
Arbitrary Argument Lists
- the * symbol indicates the following variable actually represent a list.
def fun1(*nums):nsum=0for n in nums:print(n)nsum+=nprint(nsum)
fun1(11,22,58,-458)
fun1(11,22,33,99,49)
references
- https://doc.pypy.org/