使用Multiprocessing包中的Process和Pool模块,并不断访问该_bootstrap方法的引用.
从我所看到的方法导入一个multiprocessing util.py模块,并使用它的方法(还有其他一些组合)来trace,log和manage子进程.
def _bootstrap(self):
from . import util
global _current_process
try:
self._children = set()
self._counter = itertools.count(1)
try:
sys.stdin.close()
sys.stdin = open(os.devnull)
except (OSError, ValueError):
pass
_current_process = self
util._finalizer_registry.clear()
util._run_after_forkers()
util.info('child process calling self.run()')
try:
self.run()
exitcode = 0
finally:
util._exit_function()
except SystemExit, e:
if not e.args:
exitcode = 1
elif isinstance(e.args[0], int):
exitcode = e.args[0]
else:
sys.stderr.write(str(e.args[0]) + '\n')
sys.stderr.flush()
exitcode = 1
except:
exitcode = 1 …Run Code Online (Sandbox Code Playgroud) 我只是想弄清楚这些结果背后的逻辑:
>>>nan = float('nan')
>>>nan == nan
False
# I understand that this is because the __eq__ method is defined this way
>>>nan in [nan]
True
# This is because the __contains__ method for list is defined to compare the identity first then the content?
Run Code Online (Sandbox Code Playgroud)
但在这两种情况下,我认为在幕后这个功能PyObject_RichCompareBool被称为正确?为什么会有区别?他们不应该有相同的行为吗?
当我写一些代码时,我发现了一件有趣的事情:
def test():
l = []
for i in range(10):
def f():pass
print(f)
#l.append(f)
test()
import dis
dis.dis(test)
Run Code Online (Sandbox Code Playgroud)
输出是:
<function test.<locals>.f at 0x7f46c0b0d400>
<function test.<locals>.f at 0x7f46c0b0d488>
<function test.<locals>.f at 0x7f46c0b0d400>
<function test.<locals>.f at 0x7f46c0b0d488>
<function test.<locals>.f at 0x7f46c0b0d400>
<function test.<locals>.f at 0x7f46c0b0d488>
<function test.<locals>.f at 0x7f46c0b0d400>
<function test.<locals>.f at 0x7f46c0b0d488>
<function test.<locals>.f at 0x7f46c0b0d400>
<function test.<locals>.f at 0x7f46c0b0d488>
6 0 BUILD_LIST 0
3 STORE_FAST 0 (l)
7 6 SETUP_LOOP 42 (to 51)
9 LOAD_GLOBAL 0 (range)
12 LOAD_CONST 1 …Run Code Online (Sandbox Code Playgroud) Fowler的Extract Variable重构方法,以前介绍解释变量,说使用临时变量使代码更清晰.这个想法是通过引入一个不需要的局部变量来阐明复杂的代码,并将该变量命名为出于展示目的.它还提倡对评论进行这种解释. 其他语言优化了临时变量,因此不需要时间或空间资源.为什么Python不这样做?
In [3]: def multiple_of_six_fat(n): ...: multiple_of_two = n%2 == 0 ...: multiple_of_three = n%3 == 0 ...: return multiple_of_two and multiple_of_three ...: In [4]: def multiple_of_six_lean(n): ...: return n%2 == 0 and n%3 == 0 ...:
In [5]: import dis
In [6]: dis.dis(multiple_of_six_fat)
2 0 LOAD_FAST 0 (n)
3 LOAD_CONST 1 (2)
6 BINARY_MODULO
7 LOAD_CONST 2 (0)
10 COMPARE_OP 2 (==)
13 STORE_FAST 1 (multiple_of_two)
3 16 LOAD_FAST 0 (n)
19 … 在Python中,当您有一个对象时,可以使用该int函数将其转换为整数.
例如int(1.3)将返回1.__int__在这种特殊情况下,这通过使用对象的魔术方法在内部工作float.__int__.
在Python中,Fraction对象可用于构造精确的分数.
from fractions import Fraction
x = Fraction(4, 3)
Run Code Online (Sandbox Code Playgroud)
Fraction对象缺少一个__int__方法,但你仍然可以调用int()它们并获得一个合理的整数.我想知道如何在没有__int__定义方法的情况下实现这一点.
In [38]: x = Fraction(4, 3)
In [39]: int(x)
Out[39]: 1
Run Code Online (Sandbox Code Playgroud) 迭代工具C模块是否以某种方式包含在3.x中的主Python二进制文件中?
假设构建并包含C模块,它看起来像是:
>>> import inspect
>>> import itertools
>>>
>>> inspect.getsourcefile(itertools)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 571, in getsourcefile
filename = getfile(object)
File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 518, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'itertools' (built-in)> is a built-in module
Run Code Online (Sandbox Code Playgroud)
我itertools.so在我的系统上找不到Python 3.x,但是有一个2.7.
我注意到其他一些C模块作为共享对象(locate '.so' | grep -E '^/usr/local/' | grep '.so'例如mmap.so)存在于磁盘上,那么处理的是itertools什么?如果没有共享库,我该如何使用它?
我发现了CPython实现,Python对象的结构和Python字节码.
玩函数,我发现空函数的堆栈大小为1.
为什么?声明什么var占用堆栈空间?
空功能:
def empty():
pass
Run Code Online (Sandbox Code Playgroud)
功能信息:
>>> dis.show_code(empty)
Name: empty
Filename: <pyshell#27>
Argument count: 0
Kw-only arguments: 0
Stack size: 1
Number of locals: 0
Variable names:
Constants:
0: None
Names:
Flags: OPTIMIZED, NEWLOCALS, NOFREE
First line number: 1
Free variables:
Cell variables:
Run Code Online (Sandbox Code Playgroud)
与当地人的作用:
def withlocals():
first = 0
second = [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
功能信息:
>>> dis.show_code(withlocals)
Name: withlocals
Filename: <pyshell#27>
Argument count: 0
Kw-only arguments: 0
Stack size: 3
Number of locals: 2
Variable names:
0: …Run Code Online (Sandbox Code Playgroud) 我注意到Python的字节码在这里有一个有趣的特点.
该CONTINUE_LOOP操作码取入一个target将表示循环的指令继续执行,这是一个参数FOR_ITER的指令.
该BREAK_LOOP操作码,但是,并没有做到这一点.它只会突破当前(最里面)的循环.
这背后的原因是什么?为什么它可能continue而不是break外循环?
__debug__是一个内置常量,根据常量文档,它似乎不是一个实现细节.
尝试分配给它时(文档状态不支持),将显示以下错误消息:
>>> __debug__ = False
File "<stdin>", line 1
SyntaxError: assignment to keyword
Run Code Online (Sandbox Code Playgroud)
显然,根据消息,它是一个关键字,但是:
>>> from keyword import kwlist
>>> '__debug__' in kwlist
False
Run Code Online (Sandbox Code Playgroud)
这似乎很奇怪.None,True并且False,也列为常量,包含在kwlist.
为什么不在__debug__关键字列表中?
我正在研究CPython的代码库。
我想知道在哪里可以找到表中math_sin显示的函数的定义:mathmethodsmathmodule.c
{"sin", math_sin, METH_O, math_sin_doc}
Run Code Online (Sandbox Code Playgroud)
这样做grep "math_sin" -wr的主要cpython文件夹中唯一的回报:
Modules/mathmodule.c: {"sin", math_sin, METH_O, math_sin_doc},
Run Code Online (Sandbox Code Playgroud)
在哪里可以找到此函数的定义?
python ×10
python-internals ×10
cpython ×4
bytecode ×2
python-3.x ×2
fractions ×1
int ×1
keyword ×1
loops ×1
object ×1
optimization ×1
refactoring ×1