[英]explain unfamiliar python syntax — returning into a list
我完全理解此表达式的右边在做什么。 但是它又返回了什么? 换句话说,左侧在做什么?
[records, remainder] = ''.join([remainder, tmp]).rsplit(NEWLINE,1)
我不熟悉该语法。
在此行上方将remainder
定义为空字符串:
remainder = ''
但records
未在函数中的任何位置定义。
从上下文来看,它应该累积流内容,但是我不知道如何。
records
采用rsplit
返回值的第一个元素。 remainder
的占第二位。
% python
Python 2.7 (r27:82500, Sep 16 2010, 18:02:00)
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = [3, 9]
>>> [records, remainder] = foo
>>> records
3
>>> remainder
9
>>> foo = [3, 9, 10]
>>> [records, remainder] = foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
实际上,您不需要[records, remainder]
周围的方括号,但是它们是风格上的。
不确定是否有流式传输内容,但是可以很容易地自己尝试:
>>> a = 'abc'
>>> b = '\ndef\nghi'
>>> c = (a + b).rsplit('\n', 1)
#['abc\ndef', 'ghi']
然后,它使用解压缩来分配两个变量(应写为):
fst, snd = c
( []
是多余的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.