据我所知,有三种通过理解创建生成器的方法1.
经典之一:
def f1():
g = (i for i in range(10))
Run Code Online (Sandbox Code Playgroud)
该yield变种:
def f2():
g = [(yield i) for i in range(10)]
Run Code Online (Sandbox Code Playgroud)
的yield from变体(即提出了SyntaxError除了一个函数的内部):
def f3():
g = [(yield from range(10))]
Run Code Online (Sandbox Code Playgroud)
这三种变体导致不同的字节码,这并不奇怪.第一个是最好的,这似乎是合乎逻辑的,因为它是通过理解创建生成器的专用,直接的语法.但是,它不是产生最短字节码的那个.
在Python 3.6中反汇编
经典的发电机理解
>>> dis.dis(f1)
4 0 LOAD_CONST 1 (<code object <genexpr> at...>)
2 LOAD_CONST 2 ('f1.<locals>.<genexpr>')
4 MAKE_FUNCTION 0
6 LOAD_GLOBAL 0 (range)
8 LOAD_CONST 3 (10)
10 CALL_FUNCTION 1
12 GET_ITER
14 CALL_FUNCTION 1
16 STORE_FAST 0 (g) …Run Code Online (Sandbox Code Playgroud) 由于八进制前缀现在0o在Python 3中,因此再写不合法0777.好的.
那么为什么编写00哪个评估正确0而其他数字触发语法错误是合法的呢?
>>> 01
...
File "<interactive input>", line 1
01
^
SyntaxError: invalid token
>>>
>>> 00
0
Run Code Online (Sandbox Code Playgroud) 我跑进sudo apt-get install docker.io了我的lubuntu 14.10,但它说cannot start container当我跑这个:
sudo docker run ubuntu echo "Hello World"
exec format error2015/03/16 16:31:16 Error response from daemon: Cannot start container ad265ac9c2635f42e4244472d181a2121ecba4d855fa33c10599d91aa932d2cd: exec format error
Run Code Online (Sandbox Code Playgroud)
无法找到答案,客人可能与lubuntu有关.
容器已创建,但为什么我只得到"exec格式错误"
sudo docker ps -a
e92e43115003 ubuntu:14.04 "/bin/echo 'Hello wo About an hour ago sharp_hoover
uname -a
Linux ubuntu 3.16.0-31-generic #43-Ubuntu SMP Tue Mar 10 17:41:23 UTC 2015 i686 i686 i686 GNU/Linux
Run Code Online (Sandbox Code Playgroud) 作为在正常情况下使用builtin__import__()的问题的后续内容,我进行了一些测试,并且遇到了令人惊讶的结果.
我在这里比较经典import语句的执行时间和对__import__内置函数的调用.为此,我在交互模式下使用以下脚本:
import timeit
def test(module):
t1 = timeit.timeit("import {}".format(module))
t2 = timeit.timeit("{0} = __import__('{0}')".format(module))
print("import statement: ", t1)
print("__import__ function:", t2)
print("t(statement) {} t(function)".format("<" if t1 < t2 else ">"))
Run Code Online (Sandbox Code Playgroud)
与链接问题一样,以下是导入时的比较sys以及其他一些标准模块:
>>> test('sys')
import statement: 0.319865173171288
__import__ function: 0.38428380458522987
t(statement) < t(function)
>>> test('math')
import statement: 0.10262547545597034
__import__ function: 0.16307580163101054
t(statement) < t(function)
>>> test('os')
import statement: 0.10251490255312312
__import__ function: 0.16240755669640627
t(statement) < t(function)
>>> test('threading')
import statement: 0.11349136644972191 …Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法来计算您为django项目编写的代码行?
编辑:shell的东西很酷,但在Windows上怎么样?
我是python的新手,我有一个简单的问题.
例如
如果我做
import A
Run Code Online (Sandbox Code Playgroud)
然后我可以用A.b().我想知道,如何忽略A的A.b()?
我知道这个问题可能非常基础.如果这是显而易见的事,请原谅我.考虑以下程序:
#include <stdio.h>
int main(void) {
// this is a string in English
char * str_1 = "This is a string.";
// this is a string in Russian
char * str_2 = "??? ????????? ?????????.";
// iterator
int i;
// print English string as a string
printf("%s\n", str_1);
// print English string byte by byte
for(i = 0; str_1[i] != '\0'; i++) {
printf(" %c ",(char) str_1[i]);
}
printf("\n");
// print numerical values of English string byte by byte
for(i …Run Code Online (Sandbox Code Playgroud) 模块保存字典以跟踪其上下文,例如在执行的某个点定义的名称.这本词典可以通过访问vars(module)(或module.__dict__)如果module是进口,或者通过向呼叫locals模块本身的内置功能:
更新并返回表示当前本地符号表的字典.
但是当我尝试从函数中访问locals字典时,我发现自己有点困惑.仅包含以下内容的脚本的输出是空字典:
def list_locals():
print(locals())
list_locals()
Run Code Online (Sandbox Code Playgroud)
但另一方面,如果脚本仅包含以下内容,则输出是包含的预期字典__name__,__doc__以及其他模块级变量:
print(locals())
Run Code Online (Sandbox Code Playgroud)
那么,本地词典的内容何时设置?另外,"更新"在locals函数定义中意味着什么?
既os和multiprocessing模块定义一个cpu_count函数。
os.cpu_count 记录如下:
返回系统中的 CPU 数量。如果未确定,则返回 None。
andmultiprocessing.cpu_count的文档说:
返回系统中的 CPU 数量。可能会引发 NotImplementedError。另见 os.cpu_count()
在我的机器上,它们都返回相同的结果:
>>> import os
>>> import multiprocessing as mp
>>> os.cpu_count()
8
>>> mp.cpu_count()
8
Run Code Online (Sandbox Code Playgroud)
我原以为这multiprocessing.cpu_count只是对 的引用os.cpu_count,但事实并非如此:
>>> os.cpu_count is mp.cpu_count
False
Run Code Online (Sandbox Code Playgroud)
那么它们之间有什么区别呢?我能保证他们总是返回相同的结果吗?此外,如果我想指定要为其创建的多个进程multiprocessing.Pool,我应该使用osormultiprocessing的函数吗?
我正在使用 WeasyPrint 创建一个文档。我有一些有名字的部分,其中一些可能跨越多个页面。当一个部分太长时,会发生分页符。我想要做的是重新显示当前部分的名称,最好使用相同的格式。
以下 MWE 显示了分页符后如何不显示部分标题:
<html>
<body>
<h1>First section</h1>
<p>Lorem ipsum...</p>
<p>Lorem ipsum...</p>
<p>Lorem ipsum...</p>
<p>Lorem ipsum...</p>
<p style="break-after: always;"></p>
<p>Lorem ipsum...</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
的输出weasyprint example.html example.pdf:
我想First section作为<h1>标签显示在左侧页面的顶部。
我想做这个 tex.stackexchange帖子,据我所知,它基本上包括检查当前页码是否超过当前总页数,如果超过,插入遇到的最后一节标题。
我不知道在 HTML 中这样做的可能性,它存在吗?有什么解决方法可以做到这一点吗?如果没有,是否可以让 WeasyPrint 在某个page-break钩子上执行自定义 Python 代码?
python ×8
c ×1
cpu ×1
css ×1
django ×1
docker ×1
generator ×1
html ×1
int ×1
literals ×1
namespaces ×1
performance ×1
python-os ×1
string ×1
symbol-table ×1
ubuntu ×1
weasyprint ×1