3.x中的元组参数解包 [英] Tuple parameter unpacking in 3.x

查看:73
本文介绍了3.x中的元组参数解包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-----开始PGP SIGNATURE -----

版本:GnuPG v1.4.9(GNU / Linux)

iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox + XdmeDTA KdN9Q8KZ​​AvfNe4

0 / 4AmwZGClr8zmonPAFnFsAOtHn4JhfY

= hTwE

----- END PGP SIGNATURE -----

解决方案

Martin Geisler:


ci.addCallback(lambda(ai,bi):ai * bi)



map(lambda(i,s):( field(i + 1),s),enumerate(si))

重写这些到

ci.addCallback(lambda abi:abi [0] * abi [1])



map(lambda是:(字段) (是[0] + 1),是[1]),枚举(si))

使代码更加丑陋!而且稍微长一些。



我同意很多。我可以使用sort / sorted

显示我的代码的类似示例,其中包含一个解构2或3项序列的lambda,

定义一个排序键。 br />

正如我过去所说,我希望看到更多支持模式

匹配的Python,而不是更少。来自Mathematica的人,

Scala,OcaML等,知道它们可能很有用,而Scala表明它们可能会找到一些用法。

Python也可能会找到一些用法。


我认为他们已经删除了(部分,不完全)这个Python功能

主要是为了简化CPython的C实现。


到目前为止,我认为这个删除,而不是使用{:}作为空数组文字

是在Python 3设计过程中唯一的两个错误。如果你

看看在创建Python 3本身的过程中所做的大量设计决策,我认为这是一个非常好的

结果。


再见,

bearophile


Martin Geisler< mg@daimi.au.dkwrote:


我刚尝试使用python2.6 -3运行我的代码并获得了一堆


语法警告:tuple参数解包已在3.x


警告中删除。我读过PEP-3113:

http://www.python.org/dev/peps/pep-3113/


但我仍然感到困惑,为什么你们要删除这么棒的

功能?!



我不认为很多人会在def语句中错过元组解包。


我认为警告很可能错了 - 你只需要移除

a几个parens ......


ci.addCallback(lambda(ai,bi):ai * bi)

map(lambda(i,s):( field(i + 1),s),enumerate(si))



On

Python 3.0rc1(r30rc1:66499,2008年10月4日,11:04:33)


>> f = lambda(ai,bi):ai * bi



文件"< stdin>",第1行

f = lambda(ai,bi):ai * bi

^

语法错误:语法无效




>> ; f = lambda ai,bi:ai * bi
f(2, 3)



6


同样


>> lambda(i,s):( field(i + 1),s)



文件"< stdin>",第1行

lambda(i,s):(字段(i + 1) ),s)

^

语法错误:语法无效


>> lambda i,s:(field(i + 1),s)



< function< lambdaat 0xb7bf75ec>


>>>



所以只需删除括号就可以了。


我不得不说我更喜欢命名函数,但我没有做太多

函数式编程


def f(ai,bi):

返回ai * bi

ci.addCallback(f)


def f(i,s):

返回字段(i + 1),s

map(f,enumerate(si))


PEP-3113需要更新,因为这里肯定会让人感到困惑! 2to3是

做错了也看了。


-

Nick Craig-Wood< ni **@craig-wood.com-- http://www.craig-wood。 com / nick


Nick Craig-Wood写道:


Martin Geisler< mg @ daimi.au.dkwrote:


>我刚尝试使用python2.6 -3运行我的代码并获得了一堆语法警告:tuple参数解包已被删除3.x

警告。我读过PEP-3113:

http://www.python.org/dev/peps/pep-3113/

但我仍然感到困惑,为什么你们可以删除这么棒的<功能?!



我不认为很多人会在def语句中错过元组拆包。


我认为可能是警告错了 - 你只需要删除

a几个parens ......


> ci.addCallback(lambda(ai,bi):ai * bi)
map(lambda(i,s):( field(i + 1),s),enumerate(si))



On

Python 3.0rc1(r30rc1:66499,2008年10月4日,11:04:33)

< blockquote class =post_quotes>
>>> f = lambda(ai,bi):ai * bi



File "< stdin>",第1行

f = lambda(ai,bi):ai * bi

^

语法错误:无效语法




>>> f = lambda ai,bi :ai * bi
f(2,3)



6


同样


>>> lambda(i,s):( field(i + 1),s)



文件"< stdin>",第1行

lambda(i,s):( field(i + 1), s)

^

语法错误:语法无效


>> ;> lambda i,s:(field(i + 1),s)



< function< lambdaat 0xb7bf75ec>


>>>>



所以只需删除括号即可。



不,您在此过程中更改了功能签名。


f = lambda(a,b):a * b


相当于


def f((a,b)):#double parens

返回* b


并调用f(arg)其中arg是一个可迭代的两个项目。


在3.0中它必须被重写为


def f(ab):

a,b = ab

返回* b


i。即它需要一个声明和一个表达,因此不再适用于一个lambda。


Peter


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTA KdN9Q8KZAvfNe4
0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY
=hTwE
-----END PGP SIGNATURE-----

解决方案

Martin Geisler:

ci.addCallback(lambda (ai, bi): ai * bi)
or
map(lambda (i, s): (field(i + 1), s), enumerate(si))
Rewriting these to
ci.addCallback(lambda abi: abi[0] * abi[1])
and
map(lambda is: (field(is[0] + 1), is[1]), enumerate(si))
makes the code much uglier! And slightly longer.

I agree a lot. I can show similar examples of my code with sort/sorted
that contain a lambda that de-structures sequences of 2 or 3 items, to
define a sorting key.

As I''ve stated in the past, I''d like to see more support of pattern
matching in Python, and not less. People coming from Mathematica,
Scala, OcaML, etc, know they can be useful, and Scala shows that
Python too may find some usages for that.

I think they have removed (part of, not fully) this Python feature
mostly to simplify the C implementation of CPython.

So far I think this removal, and not using {:} as empty array literal
are the only two mistakes done during the design of Python 3. If you
look at the really large number of design decisions taken during the
creation of Python 3 itself, I think this is an exceptionally good
result anyway.

Bye,
bearophile


Martin Geisler <mg@daimi.au.dkwrote:

I just tried running my code using "python2.6 -3" and got a bunch of

SyntaxWarning: tuple parameter unpacking has been removed in 3.x

warnings. I''ve read PEP-3113:

http://www.python.org/dev/peps/pep-3113/

but I''m still baffled as to why you guys could remove such a wonderful
feature?!

I don''t think many people will miss tuple unpacking in def statements.

I think the warning is probably wrong anyway - you just need to remove
a few parens...

ci.addCallback(lambda (ai, bi): ai * bi)
map(lambda (i, s): (field(i + 1), s), enumerate(si))

On
Python 3.0rc1 (r30rc1:66499, Oct 4 2008, 11:04:33)

>>f = lambda (ai, bi): ai * bi

File "<stdin>", line 1
f = lambda (ai, bi): ai * bi
^
SyntaxError: invalid syntax

But

>>f = lambda ai, bi: ai * bi
f(2,3)

6

Likewise

>>lambda (i, s): (field(i + 1), s)

File "<stdin>", line 1
lambda (i, s): (field(i + 1), s)
^
SyntaxError: invalid syntax

>>lambda i, s: (field(i + 1), s)

<function <lambdaat 0xb7bf75ec>

>>>

So just remove the parentheses and you''ll be fine.

I have to say I prefer named functions, but I haven''t done much
functional programming

def f(ai, bi):
return ai * bi
ci.addCallback(f)

def f(i, s):
return field(i + 1), s
map(f, enumerate(si))

PEP-3113 needs updating as it is certainly confusing here! 2to3 is
doing the wrong thing also by the look of it.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick


Nick Craig-Wood wrote:

Martin Geisler <mg@daimi.au.dkwrote:

> I just tried running my code using "python2.6 -3" and got a bunch of

SyntaxWarning: tuple parameter unpacking has been removed in 3.x

warnings. I''ve read PEP-3113:

http://www.python.org/dev/peps/pep-3113/

but I''m still baffled as to why you guys could remove such a wonderful
feature?!


I don''t think many people will miss tuple unpacking in def statements.

I think the warning is probably wrong anyway - you just need to remove
a few parens...

> ci.addCallback(lambda (ai, bi): ai * bi)
map(lambda (i, s): (field(i + 1), s), enumerate(si))


On
Python 3.0rc1 (r30rc1:66499, Oct 4 2008, 11:04:33)

>>>f = lambda (ai, bi): ai * bi

File "<stdin>", line 1
f = lambda (ai, bi): ai * bi
^
SyntaxError: invalid syntax

But

>>>f = lambda ai, bi: ai * bi
f(2,3)

6

Likewise

>>>lambda (i, s): (field(i + 1), s)

File "<stdin>", line 1
lambda (i, s): (field(i + 1), s)
^
SyntaxError: invalid syntax

>>>lambda i, s: (field(i + 1), s)

<function <lambdaat 0xb7bf75ec>

>>>>


So just remove the parentheses and you''ll be fine.

No, you change the function signature in the process.

f = lambda (a, b): a*b

is equivalent to

def f((a, b)): # double parens
return a*b

and called as f(arg) where arg is an iterable with two items.

In 3.0 it has to be rewritten as

def f(ab):
a, b = ab
return a*b

i. e. it needs a statement and an expression and is therefore no longer
suitable for a lambda.

Peter


这篇关于3.x中的元组参数解包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆