-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEARECAAYFAkj lQNwACgkQ6nfwy3 5F3Tj8ywCgox+Xd meDTAKdN9Q8KZAv fNe4
0/4AmwZGClr8zmonP AFnFsAOtHn4JhfY
=hTwE
-----END PGP SIGNATURE----- 21 7268
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.dk wrote:
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.dk wrote:
> 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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEARECAAYFAkj nhqMACgkQ6nfwy3 5F3TgRMwCg3kRON IU1Q33WFQQmXM1X HYlO
8hsAn1S+t8Ehtdk cY6/wKIQ4034rXPyY
=Owc1
-----END PGP SIGNATURE-----
Martin Geisler wrote:
A somewhat related question: do I pay a performance penalty when I let a
function define an inner function like this:
def foo():
def bar()
...
bar()
Some. The *code* for the body of bar is compiled as part of compiling
the body of foo, but each call of foo creates a new *function* object.
compared to just defining it once outside:
def bar():
...
def foo():
...
bar()
I'm thinking that each execution of the first foo could spend a little
time defining a new bar each time, or is that not how things work?
I realize that defining bar as an inner function has the advantage of
being able to see variables in the namespace of foo.
The alternative is to pass in the value(s) needed.
tjr
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEARECAAYFAkj n2LQACgkQ6nfwy3 5F3Til5gCdFfodP KsKC0Tn6YhLjwrc SvAf
eA8AoOix4R/H1CrTnZcpUuM771 IigfvY
=/NcI
-----END PGP SIGNATURE-----
On Sat, 04 Oct 2008 22:57:23 +0200, Martin Geisler wrote:
Dennis Lee Bieber <wl*****@ix.net com.comwrites:
>On Sat, 04 Oct 2008 13:14:40 +0200, Peter Otten <__*******@web. de> declaimed the following in comp.lang.pytho n:
>>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.
Given that most lambda's are rather short, is it really that much of a pain to just use (for the above example) ab[0]*ab[1] without unpacking?
Well -- it is because the code used to be short and sweet that it feels
ugly to me to change
lambda (a, b): a * b
into
lambda ab: ab[0] * ab[1]
The first looks perfect -- it matches the math behind the code that I am
writing. The second does not look so nice.
Here's another alternative. Compare:
>>x = (2, 3) (lambda (a,b): a*b)(x)
6
with this:
>>(lambda a,b: a*b)(*x)
6
From reading the PEP-3113 I got the impression that the author thought
that this feature was unused and didn't matter. With this I wish to say
that it matters to me.
Alas, I think it's too late. I feel your pain.
The final release of Python 3.0 hasn't been made yet. If you really care
strongly about this, you could write to the python-dev mailing list and
ask if it is worth trying to change their mind about tuple unpacking.
They'll almost certainly say no, but there's a chance it might be
reverted in 3.1.
A tiny chance.
--
Steven
On Sat, 04 Oct 2008 17:07:14 +0200, Martin Geisler wrote:
A somewhat related question: do I pay a performance penalty when I let a
function define an inner function like this:
def foo():
def bar()
...
bar()
compared to just defining it once outside:
def bar():
...
def foo():
...
bar()
I'm thinking that each execution of the first foo could spend a little
time defining a new bar each time, or is that not how things work?
I realize that defining bar as an inner function has the advantage of
being able to see variables in the namespace of foo.
That is the main advantage, followed by reducing namespace pollution, but
yes there is a very small performance penalty.
>>def outer(x):
.... return x+1
....
>>def func(x):
.... return outer(x+1)
....
>>> def func2(x):
.... def inner(x):
.... return x+1
.... return inner(x+1)
....
>>assert func(37) == func2(37) from timeit import Timer t1 = Timer('func(23) ', 'from __main__ import func') t2 = Timer('func2(23 )', 'from __main__ import func2') t1.repeat()
[1.5711719989776 611, 0.8266379833221 4355, 0.8270819187164 3066]
>>t2.repeat()
[1.8273210525512 695, 1.1913230419158 936, 1.1786220073699 951]
--
Steven
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEARECAAYFAkj ofRMACgkQ6nfwy3 5F3TgKewCfcH21Z G02FQ7gy+poLrdY Wg9K
Uh8An3cVmnYAnF3 ekoA4E9uZmOcTpd aC
=DgZp
-----END PGP SIGNATURE----- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Paul McGuire |
last post by:
I'm trying to manage some configuration data in a list of tuples, and I
unpack the values with something like this:
configList =
for data in configList:
name,a,b,c = data
... do something with a,b, and c
Now I would like to add a special fourth config value to "T":
("T",1,5,4,0.005),
|
by: Kay Schluehr |
last post by:
Hi all,
thanks for Your attention !
I think my proposal was more in mind of Rons modified exec than
Pythons lambda.
When George proposed his unpacking behavoir for list-comps as a pack of
suggar:
|
by: James Stroud |
last post by:
Hello All,
Is this a bug? Why is this tuple getting unpacked by raise? Am I missing some
subtle logic? Why does print not work the same way as raise? Both are
statements. Why does raise need to be so special?
py> sometup = 1,2
py> print sometup
(1, 2)
py> print 1,2,3, sometup
|
by: Michael Yanowitz |
last post by:
Hello:
I am still relatively new to Python. I am confused by the syntax for
tuples.
I had:
thread.start_new_thread(read_data_thread, (strDataFilename))
and got back the following error:
File "scene.py", line 256, in readData
thread.start_new_thread(read_data_thread, (strDataFilename))
|
by: harold |
last post by:
Dear all,
Maybe I stared on the monitor for too long, because I cannot find the
bug ...
My script "transition_filter.py" starts with the following lines:
import sys
for line in sys.stdin :
try :
| |
by: Tim Chase |
last post by:
Just as a pedantic exercise to try and understand Python a
bit better, I decided to try to make a generator or class
that would allow me to unpack an arbitrary number of
calculatible values. In this case, just zeros (though I
just to prove whatever ends up working, having a counting
generator would be nice). The target syntax would be
something like
>>> a,b,c = zeros()
>>> q,r,s,t,u,v = zeros()
|
by: zefciu |
last post by:
I am trying to embed a c function in my python script for a first time.
When I try to call it I get an error
SystemError: new style getargs format but argument is not a tuple
Guido said on some mailing list, that it is probably an effect of the
lack of METH_VARARGS in the functions' array, but it's ok in my source
code. Here is the full code:
#include <python2.4/Python.h>
|
by: erikcw |
last post by:
Hi,
I'm trying to build a SQL string
sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
(cid, ag, self.data)
It raises this error: AttributeError: 'tuple' object has no attribute
'encode'
|
by: beginner |
last post by:
Hi,
I am wondering how do I 'flatten' a list or a tuple? For example, I'd
like to transform or ] to .
Another question is how do I pass a tuple or list of all the
aurgements of a function to the function. For example, I have all the
arguments of a function in a tuple a=(1,2,3). Then I want to pass each
item in the tuple to a function f so that I make a function call
f(1,2,3). In perl it is a given, but in python, I haven't figured out
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |