我读到了yield扩展语法,所以如果我有:
def numgen(N):
for i in range(N):
n = yield i
if n:
yield n
Run Code Online (Sandbox Code Playgroud)
我可以考虑一下:
def numgen(N):
n = yield from range(N)
if n:
yield n
Run Code Online (Sandbox Code Playgroud)
但我注意到,如果我这样做,在编码第二台发电机后:
g = numgen(10)
next(g)
g.send(54)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in gensquare
AttributeError: 'range_iterator' object has no attribute 'send'
Run Code Online (Sandbox Code Playgroud)
那怎么样?如何将值发送到numgen生成器对象?
即使他们被弃用,没有比一个更好的模块time(即timeit),我想知道这两种功能之间的差异time.clock()和time.time().
从后者(time.time())开始,它基本上以秒为单位返回从01/01/1970(如果我没有错)经过的时间,精度一般为1秒."直到这里很容易.
相反,time.clock()仍然让我感到困惑.来自doc:
在Unix上,将当前处理器时间返回为以秒为单位的浮点数.精度,实际上是"处理器时间"含义的定义,取决于同名C函数的精度,但无论如何,这是用于对Python或时序算法进行基准测试的函数.
在Windows上,此函数返回自第一次调用此函数以来经过的挂钟秒,作为浮点数,基于Win32函数QueryPerformanceCounter().分辨率通常优于1微秒.
现在在Windows中它非常清楚time.time(),只有更好的精度才能做到这一点.
但在Unix/Linux中我无法理解.我尝试了以下代码:
import time
def procedure():
time.sleep(2.5)
# measure process time
t0 = time.clock()
procedure()
print(time.clock() - t0, "process time")
Run Code Online (Sandbox Code Playgroud)
而我得到的5.300000000000096e-05 seconds process time对我来说真的很奇怪.
阅读这个已经发布的问题答案说:
[...] time.clock()测量当前进程使用的CPU时间量.
但我无法得到它,当前进程所用的时间在做什么?当我time.clock()在Unix上调用时,我得到一个浮点数,这在那一瞬间意味着什么?过程消耗的时间从?? 至 ??
我对上述函数结果的精确度非常非常困惑.
对我来说文档根本不清楚,例如这里有两句话:
来自time模块文档
各种实时函数的精度可能低于表示其值或参数的单位所建议的精度.例如,在大多数Unix系统上,时钟"滴答"仅为每秒50或100次.
来自timeit模块文档
以特定于平台的方式定义默认计时器.在Windows上,time.clock()具有微秒粒度,但time.time()的粒度是1/60秒.在Unix上,time.clock()具有1/100秒的粒度,而time.time()则更加精确.在任一平台上,default_timer()都会测量挂钟时间,而不是CPU时间.这意味着在同一台计算机上运行的其他进程可能会干扰计时.
现在,因为实时,在Unix中,它返回time.time()并且它的分辨率远远优于1/100,它如何只能"滴答"每秒50或100次?
总是关于解决方案,我无法理解我调用每个函数的确切分辨率,所以我尝试了以下内容并在评论中进行了猜测:
>>> time.clock()
0.038955 # a resolution of microsecond?
>>> time.time()
1410633457.0955694 # a resolution of 10-7 second?
>>> time.perf_counter()
4548.103329075 # a resolution of 10-9 second (i.e nanosecond)?
Run Code Online (Sandbox Code Playgroud)
PS这是在Python3.4.0上尝试的,在Python2中time.clock(),time.time()我总是在点后得到6个数字,所以1us精度?
我只是无法得到python2.7 的功能decode()和encode()工作方式
我尝试了以下声明
>>> s = u'abcd'
>>> s.encode('utf8')
'abcd'
>>> s.encode('utf16')
'\xff\xfea\x00b\x00c\x00d\x00'
>>> s.encode('utf32')
'\xff\xfe\x00\x00a\x00\x00\x00b\x00\x00\x00c\x00\x00\x00d\x00\x00\x00'
Run Code Online (Sandbox Code Playgroud)
直到这里,我认为很清楚; encode()在相应的utf-8/16/32字节字符串中转换unicode代码.
但是当我编码:
>>> s.decode('utf8')
u'abcd'
>>> s.decode('utf16')
u'\u6261\u6463'
>>> s.decode('utf32')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/encodings/utf_32.py", line 11, in decode
return codecs.utf_32_decode(input, errors, True)
UnicodeDecodeError: 'utf32' codec can't decode bytes in position 0-3: codepoint not in range(0x110000)
Run Code Online (Sandbox Code Playgroud)
为什么decode()unicode类型的含义?为什么第一个(使用utf8)而不是后者?是因为python在内部使用utf-8存储unicode字符串吗?
最后一件事:
>>> s2 = '?'
>>> s2
'\xe2\x89\x88'
Run Code Online (Sandbox Code Playgroud)
引擎盖下会发生什么?'≈'不是ascii字符,因此python使用编码sys.getfilesystemencoding()返回隐式转换它吗?
大家好
,有可能在HTML中插入<head>结束标签之后</body>吗?或者我必须在之前<head>和之后编码<body>?
我正在w3schools.com上阅读有关css的文章,它说:
"注意:如果外部样式表的链接放在HTML内部样式表之后,外部样式表将覆盖内部样式表!"
问题是,如果外部样式表在头部中声明外部样式表,如何在内部样式表之后放置外部样式表?
我正在尝试以下方法:
>>> a = '\01'
>>> a
>>> '\x01'
>>> b = '\11'
>>> b
>>> '\t'
>>> c = '\21'
>>> c
>>> '\x11'
Run Code Online (Sandbox Code Playgroud)
我不明白为什么有时候我会得到十六进制表示而有时候却没有.
在'\ xhh'中,'x'是基本的还是不是?
我试着环顾四周,但我找不到任何关于这个话题的清楚.
是否在每次启动Python时自动导入的模块中实现内置函数?在哪个模块的情况下?
或者内置函数只是Python解释器中的嵌入式函数?
每个类直接或间接地从Object类继承.
Object除了其他人之外,这个班级有一个重要的方法,最经常被覆盖:toString.
问题是:这个方法的重写不会导致Liskov替换原则违反了Object类吗?
我会做一个例子.
public class Main
{
public static void main(String[] args)
{
Object o = new Object();
String s = o.toString();
if (s.indexOf('@') > -1) {
System.out.println("OK");
} else {
System.out.println(":-(");
}
}
}
public class MyClass
{
private int x;
public string toString()
{
return Integer.toString(x);
}
}
Run Code Online (Sandbox Code Playgroud)
显然,如果我更换new Object()与new MyClass()系统的行为变化.
One.java
public class One {
private void run() {
System.out.println("one");
}
public void start() {
this.run();
}
}
Run Code Online (Sandbox Code Playgroud)
Two.java
public class Two extends One {
public void run() {
System.out.println("two");
}
}
Run Code Online (Sandbox Code Playgroud)
Main.java
public class Main {
public static void main(String[] args) {
Two t = new Two();
t.start();
}
}
Run Code Online (Sandbox Code Playgroud)
输出:一
但是如果我在类中声明run方法public,One我会得到"两个"作为输出.
这是非常不可预测的,这是如何工作的?
现在的问题是很清楚的,为什么map()(以及zip()和filter(),但不会range()不返回一个迭代)返回一个迭代器?我的意思是我希望它只返回一个可迭代的,为什么这个选择呢?
python ×7
python-3.x ×4
java ×2
encoding ×1
generator ×1
hex ×1
html ×1
iterator ×1
linux ×1
liskov-substitution-principle ×1
python-2.7 ×1
string ×1
unicode ×1
unix ×1
yield ×1