Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Evpok Padding
On 11 April 2018 at 23:09, Brendan Barnwell wrote: > On 2018-04-11 11:05, David Mertz wrote: > >> How about this, Brendan? >> >> _, x1, x2 = (D := b**2 - 4*a*c), (-b + sqrt(D))/2, (-b - sqrt(D))/2 >> >> I'm not sure I love this, but I don't hate it. >> > > That's

[Python-ideas] Default values in multi-target assignment

2018-04-12 Thread Serhiy Storchaka
Yet one crazy idea. What if allow default values for targets in multi-target assignment? >>> (a, b=0) = (1, 2) >>> a, b (1, 2) >>> (a, b=0) = (1,) >>> a, b (1, 0) >>> (a, b=0) = () Traceback (most recent call last): File "", line 1, in ValueError: not

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Paul Moore
On 11 April 2018 at 22:28, Chris Angelico wrote: > On Thu, Apr 12, 2018 at 1:22 AM, Nick Coghlan wrote: >> This argument will be strengthened by making the examples used in the >> PEP itself more attractive, as well as proposing suitable additions to >> PEP

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Jacco van Dorp
2018-04-12 13:28 GMT+02:00 Kirill Balunov : > > > 2018-04-12 12:48 GMT+03:00 Jacco van Dorp : >> >> Wouldn't these local name bindings make the current "as" clause of >> "with f(x) as y" completely obsolete ? >> >> It's probably good to know my

Re: [Python-ideas] Default values in multi-target assignment

2018-04-12 Thread Clint Hepner
> On 2018 Apr 12 , at 5:54 a, Serhiy Storchaka wrote: > > Yet one crazy idea. What if allow default values for targets in multi-target > assignment? > >>>> (a, b=0) = (1, 2) >>>> a, b >(1, 2) >>>> (a, b=0) = (1,) >>>> a, b >(1, 0) >>>> (a, b=0)

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Kirill Balunov
2018-04-12 12:48 GMT+03:00 Jacco van Dorp : > Wouldn't these local name bindings make the current "as" clause of > "with f(x) as y" completely obsolete ? > > It's probably good to know my background, and my background is that I > know completely nothing of the

Re: [Python-ideas] Move optional data out of pyc files

2018-04-12 Thread Serhiy Storchaka
10.04.18 20:38, Chris Angelico пише: On Wed, Apr 11, 2018 at 2:14 AM, Serhiy Storchaka wrote: A deployed Python distribution generally has .pyc files for all of the standard library. I don't think people want to lose the ability to call help(), and unless I'm

Re: [Python-ideas] Move optional data out of pyc files

2018-04-12 Thread INADA Naoki
> Finally, loading docstrings and other optional components can be made lazy. > This was not in my original idea, and this will significantly complicate the > implementation, but in principle it is possible. This will require larger > changes in the marshal format and bytecode. I'm +1 on this

Re: [Python-ideas] Move optional data out of pyc files

2018-04-12 Thread Daniel Moisset
One implementation difficulty specifically related to annotations, is that they are quite hard to find/extract from the code objects. Both docstrings and lnotab are within specific fields of the code object for their function/class/module; annotations are spread as individual constants (assuming

Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Ed Kellett
On 2018-04-12 14:46, Andrés Delfino wrote: > Extending the original idea, IMHO it would make sense for the dict > constructor to create a new dictionary not only from several mappings, but > mixing mappings and iterables too. > > Consider this example: > > x = [(1, 'one')] > y = {2: 'two'} > >

Re: [Python-ideas] Add the method decorator

2018-04-12 Thread Ed Kellett
On 2018-04-12 14:57, Serhiy Storchaka wrote: > If noddy_name is a Python function, noddy.name() will call > noddy_name(noddy), but if it is a C function, noddy.name() will call > noddy_name(). > > The same is true for classes and custom callables. FWIW, you could (almost) do this in py2: >>>

[Python-ideas] Add the method decorator

2018-04-12 Thread Serhiy Storchaka
There is a difference between functions implemented in Python and C. Functions implemented in Python are descriptors. They can be used for defining methods in Python classes. Functions implemented in C are not descriptors. When set a class attribute to a functions implemented in C, it will not

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Nick Coghlan
On 12 April 2018 at 07:28, Chris Angelico wrote: > On Thu, Apr 12, 2018 at 1:22 AM, Nick Coghlan wrote: >>> Frequently Raised Objections >>> >> >> There needs to be a subsection here regarding the need to call `del` >> at class

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Chris Angelico
On Thu, Apr 12, 2018 at 11:19 PM, Nick Coghlan wrote: > On 12 April 2018 at 07:28, Chris Angelico wrote: >> On Thu, Apr 12, 2018 at 1:22 AM, Nick Coghlan wrote: Frequently Raised Objections >>> >>>

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Chris Angelico
On Thu, Apr 12, 2018 at 9:09 PM, Paul Moore wrote: > On 11 April 2018 at 22:28, Chris Angelico wrote: >> On Thu, Apr 12, 2018 at 1:22 AM, Nick Coghlan wrote: >>> This argument will be strengthened by making the examples used in the >>>

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Jacco van Dorp
2018-04-12 15:02 GMT+02:00 Nick Coghlan : > On 12 April 2018 at 22:22, Jacco van Dorp wrote: >> I've looked through PEP 343, contextlib docs ( >> https://docs.python.org/3/library/contextlib.html ), and I couldn't >> find a single case where "with (y :=

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Nick Coghlan
On 12 April 2018 at 22:22, Jacco van Dorp wrote: > I've looked through PEP 343, contextlib docs ( > https://docs.python.org/3/library/contextlib.html ), and I couldn't > find a single case where "with (y := f(x))" would be invalid. Consider this custom context manager:

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Chris Angelico
On Thu, Apr 12, 2018 at 7:21 PM, Kirill Balunov wrote: > > I gain readability! I don't see any reason to use it in other contexts... > Because it makes the code unreadable and difficult to perceive while giving > not so much benefit. I may be wrong, but so far I have not

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Chris Angelico
On Thu, Apr 12, 2018 at 11:31 PM, Jacco van Dorp wrote: > 2018-04-12 15:02 GMT+02:00 Nick Coghlan : >> On 12 April 2018 at 22:22, Jacco van Dorp wrote: >>> I've looked through PEP 343, contextlib docs ( >>>

Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Andrés Delfino
Extending the original idea, IMHO it would make sense for the dict constructor to create a new dictionary not only from several mappings, but mixing mappings and iterables too. Consider this example: x = [(1, 'one')] y = {2: 'two'} Now: {**dict(x), **y} Proposed: dict(x, y) I think this

Re: [Python-ideas] Move optional data out of pyc files

2018-04-12 Thread Daniel Moisset
I've been playing a bit with this trying to collect some data and measure how useful this would be. You can take a look at the script I'm using at: https://github.com/dmoisset/pycstats What I'm measuring is: 1. Number of objects in the pyc, and how many of those are: * docstrings (I'm using a

Re: [Python-ideas] Default values in multi-target assignment

2018-04-12 Thread Serhiy Storchaka
12.04.18 18:12, Guido van Rossum пише: I hear where you're coming from but I really don't think we should do this. If you don't have the right expectation already it's hard to guess what it means. I would much rather spend effort on a proper matching statement. There are few applications of

Re: [Python-ideas] Python-ideas Digest, Vol 137, Issue 67

2018-04-12 Thread Thautwarm Zhao
> Makes sense. However, couldn't you prevent that by giving with > priority over the binding ? As in "(with simple_cm) as value", where > we consider the "as" as binding operator instead of part of the with > statement ? Sure, you could commit suicide by parenthesis, but by > default it'd do

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Chris Angelico
On Fri, Apr 13, 2018 at 4:01 AM, Thautwarm Zhao wrote: >> > Makes sense. However, couldn't you prevent that by giving with >> priority over the binding ? As in "(with simple_cm) as value", where >> > we consider the "as" as binding operator instead of part of the with >> >

Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Mike Miller
While we're on the subject, I've tried to add dicts a few times over the years to get a new one but it doesn't work: d3 = d1 + d2 # TypeError Thinking a bit, set union is probably a better analogue, but it doesn't work either: d3 = d1 | d2 # TypeError Where the last value of any

Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Andrés Delfino
There's a long thread about the subject: https://mail.python.org/pipermail/python-ideas/2015-February/031748.html I suggest to avoid the matter altogether :) On Thu, Apr 12, 2018 at 4:15 PM, Mike Miller wrote: > While we're on the subject, I've tried to add dicts a

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread David Mertz
Fair enough. I wouldn't actually do what I suggested either. But then, I also wouldn't ever write: x1, x2 = (-b + sqrt(D)))/2, (-b - sqrt(D))/2 [with/where/whatever D=...] If your goal is simply to have symmetry in the plus-or-minus clauses, I was simply pointing out you can have that with the

Re: [Python-ideas] Proposal: A Reduce-Map Comprehension and a "last" builtin

2018-04-12 Thread Peter O'Connor
On Wed, Apr 11, 2018 at 10:50 AM, Paul Moore wrote: > In particular, I'm happiest with the named moving_average() function, > which may reflect to some extent my lack of familiarity with the > subject area. I don't *care* how it's implemented internally - an > explicit loop

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Thautwarm Zhao
> None if var:= function() is None else var.method() Make sense. In some specific scenes(maybe general, I'm not sure), var.method() if var:=function() else var looks cool although it's not really null checking. It works for Python, just like use `if lst` to check if the list `lst` is

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Alex Walters
> > On the third hand, requiring parentheses all the time would also feel > strained: > > while m := someregexp.match(somestring): > > is already impossible to misread. > > Annoying ;-) While adding parens to that would be superfluous for the reader of the module, as a tradeoff for

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Chris Angelico
On Fri, Apr 13, 2018 at 1:55 PM, Tim Peters wrote: > [David Mertz ] >> Yes, I should have added ternary expressions to if statements. I can >> definitely see the use there. >> >> However, your example is not null checking. You'd have to modify it slightly >>

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Chris Angelico
On Fri, Apr 13, 2018 at 2:37 PM, Guido van Rossum wrote: > Clearly the PEP should spell out the precedence of :=. I'm sure Chris > intended to give := the lowest possible precedence: the analogy with = (both > in Python and in other languages), and the original design of the

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread David Mertz
Yes, I have not read all the iterations of the PEP, and none of them extremely closely. I had thought it "obvious" that ':=' should have a very high operator precedence. But of course if it doesn't then expressions like the one I proposed could mean something quite different. I find the third

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Tim Peters
[David Mertz ] > Yes, I should have added ternary expressions to if statements. I can > definitely see the use there. > > However, your example is not null checking. You'd have to modify it slightly > to get that: > > None if var:= function() is None else var.method() > > Still

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Guido van Rossum
Clearly the PEP should spell out the precedence of :=. I'm sure Chris intended to give := the lowest possible precedence: the analogy with = (both in Python and in other languages), and the original design of the PEP, where the syntax was ( as ), with mandatory parentheses. (Probably his

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread David Mertz
Yes, I should have added ternary expressions to if statements. I can definitely see the use there. However, your example is not null checking. You'd have to modify it slightly to get that: None if var:= function() is None else var.method() Still not bad looking. On Thu, Apr 12, 2018, 11:01 PM

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Chris Angelico
On Fri, Apr 13, 2018 at 2:14 PM, David Mertz wrote: > Yes, I have not read all the iterations of the PEP, and none of them > extremely closely. I had thought it "obvious" that ':=' should have a very > high operator precedence. But of course if it doesn't then expressions like >

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Tim Peters
[David Mertz ] > Yes, I have not read all the iterations of the PEP, and none of them > extremely closely. I had thought it "obvious" that ':=' should have a very > high operator precedence. But you don't really believe that ;-) That's the rub. In your specific None if

Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Serhiy Storchaka
12.04.18 22:42, Andrés Delfino пише: I think the update method can (and personally, should) stay unchanged: spam.update(dict(x, y)) seems succinct and elegant enough, with the proposed constructor syntax. Sorry my ignorance, do (Mutable)Mapping ABC say anything about constructors? Mapping

Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Guido van Rossum
On Thu, Apr 12, 2018 at 7:34 AM, Ed Kellett wrote: > On 2018-04-12 14:46, Andrés Delfino wrote: > > Extending the original idea, IMHO it would make sense for the dict > > constructor to create a new dictionary not only from several mappings, > but > > mixing mappings

Re: [Python-ideas] Default values in multi-target assignment

2018-04-12 Thread Guido van Rossum
I hear where you're coming from but I really don't think we should do this. If you don't have the right expectation already it's hard to guess what it means. I would much rather spend effort on a proper matching statement. On Thu, Apr 12, 2018 at 2:54 AM, Serhiy Storchaka

Re: [Python-ideas] Move optional data out of pyc files

2018-04-12 Thread Giampaolo Rodola'
On Fri, 13 Apr 2018 at 03:47, M.-A. Lemburg wrote: > I think moving data out of pyc files is going in a wrong direction: > more stat calls means slower import and slower startup time. > > Trying to make pycs smaller also isn't really worth it (they > compress quite well). > >

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Guido van Rossum
On Thu, Apr 12, 2018 at 8:22 PM, David Mertz wrote: > Yes, I should have added ternary expressions to if statements. I can > definitely see the use there. > > However, your example is not null checking. You'd have to modify it > slightly to get that: > > None if var:= function()

[Python-ideas] Proposal: flatten multi-nested list/tuple/set and other certain class/type.

2018-04-12 Thread delta114514
I thought that itertools.chain.from_iterable isn't useful. cuz this only allow "single-nested iterable -- this will raise error when arg has non-nested element--" like below:: >>> from itertools import chain >>> chain.from_iterable([1]) >>> list(_) Traceback (most recent call last): File "",

[Python-ideas] flatten multi-nested list/set/tuple and certain types.

2018-04-12 Thread delta114514
I thought that itertools.chain.from_iterable isn't useful. cuz this only allow "single-nested iterable -- this will raise error when arg has non-nested element--" like below:: >>> from itertools import chain >>> chain.from_iterable([1]) >>> list(_) Traceback (most recent call last): File "",

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Stephen J. Turnbull
Nick Coghlan writes: > > # Similar to the boolean 'or' but checking for None specifically > > x = "default" if (eggs := spam().ham) is None else eggs > > > > # Even complex expressions can be built up piece by piece > > y = ((eggs := spam()), (cheese := eggs.method()),

Re: [Python-ideas] PEP 572: Assignment Expressions (post #4)

2018-04-12 Thread Chris Angelico
On Thu, Apr 12, 2018 at 4:45 PM, Michel Desmoulin wrote: > > > Le 11/04/2018 à 23:34, George Leslie-Waksman a écrit : >> I really like this proposal in the context of `while` loops but I'm >> lukewarm in other contexts. >> >> I specifically like what this would do for