dis当我注意到为模块生成的字节码时,我正在使用模块的Python字节码.给出一个名为的小测试模块mod.py:
if __name__ == "__main__":
print("Hello World")
Run Code Online (Sandbox Code Playgroud)
我用它为compile()函数创建了一个代码对象:
>>> cobj = compile(open("mod.py").read(), "mod", "exec")
Run Code Online (Sandbox Code Playgroud)
然后,反汇编代码对象以查看字节码:
>>> dis.dis(cobj)
1 0 LOAD_NAME 0 (__name__)
3 LOAD_CONST 0 ('__main__')
6 COMPARE_OP 2 (==)
9 POP_JUMP_IF_FALSE 22
2 12 LOAD_NAME 1 (print)
15 LOAD_CONST 1 ('Hello World')
18 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
21 POP_TOP
>> 22 LOAD_CONST 2 (None)
25 RETURN_VALUE
Run Code Online (Sandbox Code Playgroud)
让我印象深刻的是最后两个命令LOAD_CONST和RETURN_VALUE,如果我的理解是正确的,将常None开值栈,因此返回.
模块是否隐式返回None,如果是,为什么?
def apply_twice(func,arg):
return func(func(arg))
def add_five(x):
return x+5
print (apply_twice(add_five,10))
Run Code Online (Sandbox Code Playgroud)
我得到的输出是20.
这个实际上让我很困惑,比如它是如何工作的.任何人都可以通过分解来解释我的工作方式
我scikit-learn使用命令安装conda install scikit-learn,它在我的anaconda安装文件夹下可用.
但是,运行python(Python 3.5.2 | Anaconda自定义(64位))后,我得到:
ImportError: No module named 'scikit'
Run Code Online (Sandbox Code Playgroud)
我有环境变量:
PYTHONHOME = C:\Users\Lejla\Anaconda3
PYTHONPATH = C:\Users\Lejla\Anaconda3\Lib
Run Code Online (Sandbox Code Playgroud)
我也把这些包括在我的PATH:
C:\Users\Lejla\Anaconda3;C:\Users\Lejla\Anaconda3\Scripts;C:\Users\Lejla\Anaconda3\Library\bin;
Run Code Online (Sandbox Code Playgroud) 使用Python执行时,它显示错误:
return (x * (1.0 — x))
^
SyntaxError: invalid character in identifier
Run Code Online (Sandbox Code Playgroud)
我该如何纠正?
我的专栏:
urlpatterns = [
url(r'^signout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
...
...
Run Code Online (Sandbox Code Playgroud)
警告:
RemovedInDjango110Warning: Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got django.contrib.auth.views.logout). Pass the callable instead.
2016-08-23T14:56:28.580019+00:00 app[jobQueue.1]: url(r'^signout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
Run Code Online (Sandbox Code Playgroud)
我该怎么用这条线代替?
>>> L = [1, 2, 3, 4]
>>> L[0:2] = [8, 9]
>>> L
[8, 9, 3, 4]
>>> L[0:2].reverse()
>>> L
[8, 9, 3, 4]
Run Code Online (Sandbox Code Playgroud)
你能解释一下为什么L[0:2].reverse()不改变L清单吗?
我的代码给我一个错误,说第 9 行的索引超出范围。这是我的代码:
s = 'aba'
letter = ''
substring = ''
i = 0
while(i <= len(s)):
prev_letter = s[0]
letter = s[i]
if letter <= prev_letter:
substring += letter
prev_letter = letter
i += 1
Run Code Online (Sandbox Code Playgroud) python ×7
python-3.x ×7
python-2.7 ×2
anaconda ×1
bytecode ×1
django ×1
list ×1
module ×1
reverse ×1
scikit-learn ×1
windows ×1