[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread MRAB
On 2023-10-21 21:15, Chris Angelico wrote: On Sun, 22 Oct 2023 at 06:37, Ram Rachum wrote: On Sat, Oct 21, 2023 at 10:30 PM Chris Angelico wrote: > I love that, but it mostly makes sense for "if there's a match do this, otherwise do that" where most cases fall into "I'm absolutely sure th

[Python-ideas] Re: SyntaxError: cannot use assignment expressions with attribute

2023-10-08 Thread MRAB
On 2023-10-09 02:17, Dom Grigonis wrote: Is there a reason why this is not allowed? return (self.mode := self.mode_valid(mode)) The principal use-case for the operator is in conditions, for example: if m := re.match(pattern_1, string): ... elif m := re.match(pattern_2, strin

[Python-ideas] Re: Extract variable name from itself

2023-09-13 Thread MRAB
On 2023-09-12 16:54, Rob Cliffe via Python-ideas wrote: On 12/09/2023 11:54, Dom Grigonis wrote: Yes, Thank you! So 2 solutions now. They both solve what I have encountered. Beyond that, they differ by: a) f-string print(f’{=a.method}’) # ‘a.method’ No new builtin needed.

[Python-ideas] Re: Proposal for get_or function in Python dictionaries

2023-07-17 Thread MRAB
On 2023-07-17 18:10, Dom Grigonis wrote: It does exactly the same as the C version and is more readable.  Or am I missing something? My point is exactly that it is not easily readable compared to C version. Also, unnecessarily verbose. The order of components is rather awkward. I came to

[Python-ideas] Re: dict method to retrieve a key from a value

2023-07-02 Thread MRAB
On 2023-07-02 17:14, Stephen J. Turnbull wrote: MRAB writes: > I would hope that if the maintainer knew that the package was on > the curated list, they would let the list know if they were going > to stop maintaining it. That would be part of being on the list - > informin

[Python-ideas] Re: dict method to retrieve a key from a value

2023-07-02 Thread MRAB
On 2023-07-02 06:59, Chris Angelico wrote: On Sun, 2 Jul 2023 at 15:48, Christopher Barker wrote: Only re (the one in the stdlib)? What if you want PCREs - there's no package called "pcre" but there's "pcre2", "python-pcre", and probably others. And are those three (and others?) actually use

[Python-ideas] Re: dict method to retrieve a key from a value

2023-07-01 Thread MRAB
On 2023-07-01 11:22, Paul Moore wrote: On Sat, 1 Jul 2023 at 07:09, Christopher Barker > wrote: So I think light curation would help a lot. [*] I'd be completely in favour of someone setting up a curated package index. You could probably use the Warehouse codeba

[Python-ideas] Re: else without except

2023-06-30 Thread MRAB
On 2023-06-30 14:55, Daniel Walker wrote: As most of you probably know, you can use else with try blocks: try:     do_stuff() except SomeExceptionClass:    handle_error() else:    no_error_occurred() Here, no_error_occurred will only be called if do_stuff() didn't raise an exception. Howe

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread MRAB
On 2023-06-13 16:42, Chris Angelico wrote: On Wed, 14 Jun 2023 at 01:07, BoppreH via Python-ideas wrote: And I have to say I'm surprised by the responses. Does nobody else hit bugs like this and wish they were automatically detected? Nope, I've never had that happen to me, and I *have* made

[Python-ideas] Re: @lazy decorator an alternative to functools.partial ?

2023-05-18 Thread MRAB
On 2023-05-19 00:54, Greg Ewing wrote: On 19/05/23 4:16 am, Christopher Barker wrote: The problem with "batteries included" is that what exactly is a battery is hard to define, and everyone has a different opinion about it. To my mind, "batteries included" means you can do *something* useful w

[Python-ideas] Re: Allowing `str.format` to format one or more parameters instead of all parameters

2023-04-29 Thread MRAB
On 2023-04-29 08:31, Matsuoka Takuo wrote: On Sat, 29 Apr 2023 at 00:52, MRAB wrote: What happens if you do '{open}...{close}'.partial_format(open='{close}'? You get '{close}...{close}', and you're going to have a problem using that as a format string an

[Python-ideas] Re: Allowing `str.format` to format one or more parameters instead of all parameters

2023-04-28 Thread MRAB
On 2023-04-28 15:25, Matsuoka Takuo wrote: What if it's done not by format method but by a separete method, say, format_partially or something? The method is different from format also in that it should leave "{{" and "}}" unaltered. On Fri, 28 Apr 2023 at 00:05, Matthias Görgens wrote: Does

[Python-ideas] Re: Allowing `str.format` to format one or more parameters instead of all parameters

2023-04-26 Thread MRAB
On 2023-04-26 17:41, Alexandre Brault wrote: On 2023-04-26 11:03 a.m., MRAB wrote: On 2023-04-26 02:16, Joao S. O. Bueno wrote: On Sat, Apr 22, 2023 at 10:06 AM Damian Cross <mailto:damnedbo...@gmail.com>> wrote:     That would have the effect that every use of str.format for

[Python-ideas] Re: Allowing `str.format` to format one or more parameters instead of all parameters

2023-04-26 Thread MRAB
On 2023-04-26 02:16, Joao S. O. Bueno wrote: On Sat, Apr 22, 2023 at 10:06 AM Damian Cross > wrote: That would have the effect that every use of str.format for everyone would start producing partially-formatted strings if an argument is accidentally omit

[Python-ideas] Re: Combinations of keywords

2023-02-21 Thread MRAB
On 2023-02-21 11:26, Steven D'Aprano wrote: [snip] If your code blocks are a single statement, you can write: try: import windows_module except ImportError: import unix_module but most people will say that is ugly and be spread out: try: import windows_module excep

[Python-ideas] Re: Add a line_offsets() method to str

2022-06-20 Thread MRAB
On 2022-06-20 16:12, Christopher Barker wrote: Hmm - I’m a bit confused about how you handle mixed / multiple line endings. If you use splitlines(), then it will remove the line endings, so if there are two-char line endings, then you’ll get off by one errors, yes? I would think you could loo

[Python-ideas] Re: Bare wildcard in de-structuring to ignore remainder and stop iterating (restart)

2022-06-20 Thread MRAB
On 2022-06-20 15:05, Chris Angelico wrote: On Mon, 20 Jun 2022 at 21:11, Jonathan Fine wrote: Hi Some have liked adding a new syntax a, b, ... = iterable to mean consume two items from the iterable. However, a, b, Ellipsis = iterable has a different meaning (at least in Python 3.8). It

[Python-ideas] Re: Add a line_offsets() method to str

2022-06-18 Thread MRAB
On 2022-06-18 21:55, Jonathan Slenders wrote: Good catch! One correction here, I somewhat mixed up the benchmarks. I forgot both projects of mine required support for universal line endings exactly like splitlines() does this out of the box. That requires a more complex regex pattern. I was act

[Python-ideas] Re: Bare wildcard in de-structuring to ignore remainder and stop iterating (restart)

2022-06-17 Thread MRAB
On 2022-06-17 14:10, Jonathan Fine wrote: Hi Consider     >>> a, b, *_ = iter('abdef')     >>> a, b, None = iter('abdef')       File "", line 1     SyntaxError: can't assign to keyword If providing this feature is found to be a good idea, it might be better to use 'None' or even a new keyw

[Python-ideas] Re: Addition to fnmatch.py

2022-06-07 Thread MRAB
On 2022-06-07 11:03, Chris Angelico wrote: On Tue, 7 Jun 2022 at 19:09, Ben Rudiak-Gould wrote: This calls the predicate once per element: def partition(pred, iterable): t1, t2 = tee((pred(x), x) for x in iterable) return (x for b, x in t1 if not b), (x for b, x in t2 if b

[Python-ideas] Re: Addition to fnmatch.py

2022-06-05 Thread MRAB
On 2022-06-05 16:12, David Mertz, Ph.D. wrote: This is exactly the problem solved by set difference. E.g. `{a, b, c} - {a, c}`. This operation is linear on the size of the removed set. You don't have to use a set difference, which might matter if the order was important, but just make a set o

[Python-ideas] Re: Awaiting until a condition is met

2022-05-15 Thread MRAB
On 2022-05-16 00:47, Steven D'Aprano wrote: On Sun, May 15, 2022 at 03:34:15PM +0100, MRAB wrote: On 2022-05-15 03:10, Aaron Fink wrote: >Hello. > >I've been thinking it would be nice to be able to use await to suspend >execution until a condition is met (as repres

[Python-ideas] Re: Awaiting until a condition is met

2022-05-15 Thread MRAB
On 2022-05-15 03:10, Aaron Fink wrote: Hello. I've been thinking it would be nice to be able to use await to suspend execution until a condition is met (as represented by a function or lambda which returns a boolean. Currently, as far as I can tell, the way to accomplish this is with a whil

[Python-ideas] Re: Time to relax some restrictions on the walrus operator?

2022-05-08 Thread MRAB
On 2022-05-08 13:44, Chris Angelico wrote: On Sun, 8 May 2022 at 22:09, Valentin Berlier wrote: A while ago there was a discussion about allowing "match" patterns for the walrus operator. This would cover iterable unpacking as you suggested along with all the patterns allowed in match statem

[Python-ideas] Re: Time to relax some restrictions on the walrus operator?

2022-05-08 Thread MRAB
On 2022-05-08 11:50, Steven D'Aprano wrote: I don't have an opinion one way or the other, but there is a discussion on Discourse about the walrus operator: https://discuss.python.org/t/walrus-fails-with/15606/1 Just a quick straw poll, how would people feel about relaxing the restriction on th

[Python-ideas] Re: Custom literals, a la C++

2022-04-11 Thread MRAB
On 2022-04-11 20:01, Chris Angelico wrote: [snip] Which raises the question: what if the current directory no longer has a path name? Or is that simply not possible on Windows? I know that on Linux, I can unlink a directory while being in it (which creates interesting problems for bash, git, and

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread MRAB
On 2022-03-19 02:38, Steven D'Aprano wrote: Hi Nathan, and welcome! On Fri, Mar 18, 2022 at 05:13:16AM -, Nathan Levett wrote: First time posting here ~ I've recently encountered that python does not have an OOTB operator for modulo that is consistent with Euclidean division. Although it

[Python-ideas] Re: Add a replace method to tuples

2022-03-15 Thread MRAB
On 2022-03-15 23:13, Steven D'Aprano wrote: On Tue, Mar 15, 2022 at 11:16:04AM +1100, Chris Angelico wrote: > How do we make a new list with a change other than the same slice and > concatenation we use with tuples? > > alist[:i] + [value] + alist[i+1:] > > I mean as an expression, of cours

[Python-ideas] Re: Using "Get Satisfaction" for Python Suggestions

2022-02-22 Thread MRAB
On 2022-02-20 17:56, Stephen J. Turnbull wrote: Gerrit Holl writes: > If voting is limited to a select group (which could be as small as > Python core developers, or as large as anyone who has ever had a pull > request merged into cpython, or something in-between), then a vote > could be

[Python-ideas] Re: Regex timeouts

2022-02-16 Thread MRAB
s.python.org/issue2636 In 2021 a core dev eventually rejected it, as by then MRAB had long since released it as a successful extension module. I assume - but don't know - he got burned out by "the endless bikeshedding" on those issue reports. I eventually decided against having

[Python-ideas] Re: Regex timeouts

2022-02-15 Thread MRAB
On 2022-02-16 02:11, Chris Angelico wrote: On Wed, 16 Feb 2022 at 12:56, Tim Peters wrote: Regexps keep "evolving"... Once upon a time, a "regular expression" was a regular grammar. That is no longer the case. Once upon a time, a regular expression could be broadly compatible with multiple d

[Python-ideas] Re: Regex timeouts

2022-02-15 Thread MRAB
On 2022-02-15 06:05, Tim Peters wrote: [Steven D'Aprano ] I've been interested in the existence of SNOBOL string scanning for a long time, but I know very little about it. How does it differ from regexes, and why have programming languages pretty much standardised on regexes rather than other f

[Python-ideas] Re: Missing expandvars equivalent in pathlib

2022-02-11 Thread MRAB
On 2022-02-11 20:05, Ricky Teachey wrote: Just had a thought kernel: what if there were an f-string mini-language directive to grab environment variables and expand user paths? That seems to me like it could be even more useful beyond just working with paths. Maybe something like: f"{my_var:

[Python-ideas] Re: "frozen" operator Re: Revisiting a frozenset display literal

2022-01-20 Thread MRAB
On 2022-01-21 00:18, Cameron Simpson wrote: Well, I've just waded through this discussion. This all feels to me like a special case of "wanting a constant for bytecode". What is we had a "freeze" operator, eg: |foo| which would produce a frozen version of foo. I'm liking the vertical bar

[Python-ideas] Re: Allowing non-ASCII bracket and quote characters in source code

2022-01-18 Thread MRAB
On 2022-01-19 00:02, Steven D'Aprano wrote: On Wed, Jan 19, 2022 at 10:12:23AM +1100, Chris Angelico wrote: Not sure about Python, but C and C++ have digraphs and trigraphs as alternatives for certain symbols, specifically because some OS/keyboard/language combinations may not be able to easily

[Python-ideas] Re: Revisiting a frozenset display literal

2022-01-18 Thread MRAB
On 2022-01-18 23:50, Steven D'Aprano wrote: On Wed, Jan 19, 2022 at 11:30:36AM +1300, Greg Ewing wrote: I'd also be happy with making frozenset a keyword. - int, float, str, tuple, dict, set, exceptions, len, etc are not keywords, so they can be shadowed (for good or bad); - alone out of

[Python-ideas] Re: Revisiting a frozenset display literal

2022-01-18 Thread MRAB
On 2022-01-18 18:54, Neil Girdhar wrote: Even if f{1} creates a frozenset, I don't think f{} should create a frozenset.  I think it makes more sense to keep f{1: 2} open for frozendict if it ever makes it in.  Also, {} should be consisten with f{} (both should create dicts).  If you want an emp

[Python-ideas] Re: Revisiting a frozenset display literal

2022-01-17 Thread MRAB
On 2022-01-17 06:07, Greg Ewing wrote: U+2744 Snowflake, anyone? my_frozenset = ❄{1, 2, 3} That makes me think about using '@': my_frozenset = @{1, 2, 3} It's currently used as a prefix for decorators, but that's at the start of a line. It could be a problem for the REPL, though: >>>

[Python-ideas] Re: Shouldn't 'input' prompt be going to stderr?

2022-01-16 Thread MRAB
On 2022-01-17 03:11, Cameron Simpson wrote: On 16Jan2022 20:57, MRAB wrote: There was a discussion on another forum that mentioned the 'input' function, and it made me wonder whether 'input' should be printing the prompt to stderr instead of stdout. The convention is for

[Python-ideas] Re: Revisiting a frozenset display literal

2022-01-16 Thread MRAB
On 2022-01-17 01:36, Steven D'Aprano wrote: On Sun, Jan 16, 2022 at 04:23:47PM -0800, Brendan Barnwell wrote: On 2022-01-16 16:11, Steven D'Aprano wrote: >Do they? How are they different? You have a start delimiter and an end >delimiter. > [snip] Hell, even if your argument is just "Nope, I j

[Python-ideas] Shouldn't 'input' prompt be going to stderr?

2022-01-16 Thread MRAB
There was a discussion on another forum that mentioned the 'input' function, and it made me wonder whether 'input' should be printing the prompt to stderr instead of stdout. The convention is for normal output to go to stdout and error messages to go to stderr, so if a program needs to ask the

[Python-ideas] Re: Revisiting a frozenset display literal

2022-01-16 Thread MRAB
On 2022-01-16 08:27, Steven D'Aprano wrote: [snip] dis.dis("frozenset({1, 2, 3})") 1 0 LOAD_NAME0 (frozenset) 2 BUILD_SET0 4 LOAD_CONST 0 (frozenset({1, 2, 3})) 6 SET_UPDATE 1

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-25 Thread MRAB
On 2021-12-25 23:52, Steven D'Aprano wrote: On Thu, Dec 23, 2021 at 05:53:46PM -, Stefan Pochmann wrote: Chris Angelico wrote: > If you're removing multiple, it's usually best to filter. This is a > great opportunity to learn about list comprehensions and the > difference between O(n) and O(

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-21 Thread MRAB
On 2021-12-22 01:53, Rob Cliffe via Python-ideas wrote: Currently list.remove raises ValueError if the element is not in the list. When called with the count argument, should it raise ValueError if there are fewer than `count` occurrences of the element in the list? There's str.replace and str.s

[Python-ideas] Re: PEP 671 (late-bound arg defaults), next round of discussion!

2021-12-12 Thread MRAB
On 2021-12-12 23:49, Steven D'Aprano wrote: On Sun, Dec 12, 2021 at 12:07:30PM -0700, Carl Meyer wrote: I don't think this is a fair dismissal of the concern. Taken broadly, function "wrapping" is extremely common, in the sense that what many (most?) functions do is call other functions, and th

[Python-ideas] Re: PEP 671 (late-bound arg defaults), next round of discussion!

2021-12-08 Thread MRAB
On 2021-12-08 23:39, Chris Angelico wrote: On Thu, Dec 9, 2021 at 10:35 AM Paul Moore wrote: On Wed, 8 Dec 2021 at 23:18, Chris Angelico wrote: > Part of the problem is that it is really REALLY hard to figure out > what the actual objections are. I asked, and the one clear answer I > got was

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults -- choice of -> vs @

2021-11-03 Thread MRAB
On 2021-11-03 18:14, Ethan Furman wrote: On 11/3/21 10:35 AM, Steven D'Aprano wrote: > I suppose that we could even add yet another overloaded meaning on the > asterix: > > # with no default, * keeps the old meaning of collecting > # extra positional values > > *para

[Python-ideas] Re: Real Positional Arguments or OO Currying

2021-10-18 Thread MRAB
On 2021-10-18 15:58, Steven D'Aprano wrote: On Mon, Oct 18, 2021 at 10:20:17AM -, Mathew Elman wrote: despite a driving idea of python syntax being readability in english, the function signature is distinctly not english. Python's syntax was not really modelled on English, as far as I can

[Python-ideas] Re: Real Positional Arguments or OO Currying

2021-10-18 Thread MRAB
On 2021-10-18 11:20, Mathew Elman wrote: I don't know if this has been suggested before, or if this is outlandishly impossible (though I would be surprised if it was), so apologies in advance if so. I have on occasion come across a situation where I use/write a signature like this: def

[Python-ideas] Re: Unpacking in tuple/list/set/dict comprehensions

2021-10-16 Thread MRAB
On 2021-10-16 20:31, Erik Demaine wrote: On Sun, 17 Oct 2021, Steven D'Aprano wrote: On Sat, Oct 16, 2021 at 11:42:49AM -0400, Erik Demaine wrote: I guess the question is whether to define `(*it for it in its)` to mean tuple or generator comprehension or nothing at all. I don't see why that

[Python-ideas] Re: Implementing additional string operators

2021-10-13 Thread MRAB
On 2021-10-13 16:26, Marc-Andre Lemburg wrote: On 13.10.2021 17:11, Guido van Rossum wrote: Maybe we should only accept operators as aliases for existing methods. x-y could mean x.removesuffix(y) That was the idea, yes, in particular to make it similar to "+", which adds to the end of the str

[Python-ideas] Re: Overloading unary plus in strings with "ord"

2021-10-12 Thread MRAB
On 2021-10-12 23:57, Steven D'Aprano wrote: On Wed, Oct 13, 2021 at 09:22:09AM +1100, Chris Angelico wrote: Mathematicians and programmers both extend operators to new meanings, but only where it makes sense. In fairness, mathematicians typically just invent new symbols, when they're not repu

[Python-ideas] Re: Overloading unary plus in strings with "ord"

2021-10-12 Thread MRAB
On 2021-10-12 13:49, Steven D'Aprano wrote: On Tue, Oct 12, 2021 at 11:36:42PM +1100, Chris Angelico wrote: You haven't given any reason why unary plus should imply ord(). I think the question Chris is really asking is why should unary plus return ord() rather than any other function or metho

[Python-ideas] Re: Overloading unary plus in strings with "ord"

2021-10-12 Thread MRAB
On 2021-10-12 13:36, Chris Angelico wrote: On Tue, Oct 12, 2021 at 11:27 PM Jeremiah Vivian wrote: > -1. It's unnecessary optimization for an uncommon case, abuse of syntax Good point. But what else can the unary positive do? I'm just trying to add a use for it. > illogical - why should +"a"

[Python-ideas] Re: Overloading unary plus in strings with "ord"

2021-10-12 Thread MRAB
On 2021-10-12 13:25, Jeremiah Vivian wrote: -1. It's unnecessary optimization for an uncommon case, abuse of syntax Good point. But what else can the unary positive do? I'm just trying to add a use for it. illogical - why should +"a" be the integer 97? Because `ord("a")` is `97`. Have you rea

[Python-ideas] Re: Allow regex group name redefinitions

2021-10-02 Thread MRAB
On 2021-10-02 10:27, ven...@razdva.cz wrote: Hello everybody, I've got a suggestion for the std. re module developers: to consider allowing match group name redefinitions, especially in alternatives. [snip] FYI, the regex module on PyPI supports that. __

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-10-02 Thread MRAB
es are optional if there's only one parameter, so:     () => ...     (x) => ... or x => ...     (x, y) => ...     (x, y, z) => ... etc. > On 30 Sep 2021, at 7:53 PM, MRAB wrote: > > On 2021-09-30 07:21, Chris Angelico wrote: >>> On Thu, Se

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-30 Thread MRAB
On 2021-09-30 07:21, Chris Angelico wrote: On Thu, Sep 30, 2021 at 4:19 PM Steven D'Aprano wrote: On Wed, Sep 29, 2021 at 02:09:03PM -0700, Guido van Rossum wrote: > Over in typing-sig we're considering a new syntax for callable *types*, > which would look like (int, int, str) -> float. A matc

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread MRAB
On 2021-09-29 10:11, Dominik Vilsmeier wrote: Lambda functions that have a single parameter are a common thing, e.g. for "key" functions: `sorted(items, key=lambda x: x['key'])`. For these cases however, the rather long word "lambda" together with the repetition of the parameter name, results i

[Python-ideas] Re: Different exceptions for assert

2021-09-09 Thread MRAB
On 2021-09-09 22:31, Juancarlo Añez wrote: Well, if the idea makes sense, then I'm certain that we'll have a very long and productive discussion about the best syntax here (re: *:=*). ;-) For backwards compatibility and no surprises: *assert: *ExType, cond, args It doesn't break anythi

[Python-ideas] Re: Allow starred expressions to be used as subscripts for nested lookups for getitem and setitem

2021-09-01 Thread MRAB
On 2021-09-01 17:41, Kevin Mills wrote: Given code like this: ``` d = {1: {2: {3: 4}}} print(d[1][2][3]) d[1][2][3] = None print(d) ``` It should be possible to rewrite it using a starred expression, like this: ``` d = {1: {2: {3: 4}}} keys= 1,2,3 print(d[*keys]) d[*keys] = None print(d) ```

[Python-ideas] Re: NAN handling in statistics functions

2021-08-29 Thread MRAB
On 2021-08-30 04:31, Steven D'Aprano wrote: On Sun, Aug 29, 2021 at 08:20:07PM -0400, tritium-l...@sdamon.com wrote: Not to go off on too much of a tangent, but isn't NaN unorderable? Its greater than nothing, and less than nothing, so you can't even really sort a list with a NaN value in it (

[Python-ideas] Re: We should have an explicit concept of emptiness for collections

2021-08-24 Thread MRAB
On 2021-08-25 00:48, Guido van Rossum wrote: Hi Tim, I'm sorry if this has been brought up before, but *aside from PEP 8* is there anything wrong with using "if len(a)" for nonempty, or "if not len(a)" for empty? What is the cost of 'len'? If it's always O(1), then it's not a problem, but i

[Python-ideas] Re: We should have an explicit concept of emptiness for collections

2021-08-22 Thread MRAB
en to stop: len(collection) == 0 (len(collection) == 0) is True ((len(collection) == 0) is True) is True (((len(collection) == 0) is True) is True) is True len(collection) == 0) is True) is True)) is True # ... *wink* MRAB and Ricky: `__builtins__` is a CPython impl

[Python-ideas] Re: We should have an explicit concept of emptiness for collections

2021-08-22 Thread MRAB
On 2021-08-22 17:36, Thomas Grainger wrote: bool((len(collection) == 0) is True) == True and issubclass(True, bool) 'True' is a reserved word, so you don't need to check it. However, 'bool' might have been overridden, so: __builtins__.bool((len(collection) == 0) is True) == True Come to thin

[Python-ideas] Re: Definition of a starred expression in the Language Reference

2021-08-22 Thread MRAB
On 2021-08-22 11:51, Matsuoka Takuo wrote: Dear developers, According to the Language Reference, a starred expression is defined by starred_expression ::= expression | (starred_item ",")* [starred_item] https://docs.python.org/3/reference/expressions.html#expression-lists However, in view

[Python-ideas] Re: builtins for running context managers

2021-07-16 Thread MRAB
On 2021-07-16 12:44, Stephen J. Turnbull wrote: Thomas Grainger writes: > Another example, is a cash point (ATM) won't give you your money > until you take your card That ATM is effective in enforcing the desired behavior. In Python you would usually use an exception to force handling. Re

[Python-ideas] Re: writelines2?

2021-07-13 Thread MRAB
On 2021-07-13 15:18, Eric V. Smith wrote: On 7/13/2021 9:52 AM, Thomas Güttler wrote: Am Di., 13. Juli 2021 um 15:02 Uhr schrieb >: Right now, writelines is a very big misnomer to many python developers, especially beginners who would expect writelines

[Python-ideas] Re: writelines2?

2021-07-13 Thread MRAB
On 2021-07-13 15:11, sandhoners...@gmail.com wrote: Maybe (to be consistent with other functions like print), end= since that would allow even custom line endings I'm not sure about the name 'end'. The 'print' function has 'end', but it's printed only at the end(!); .writelines would write it

[Python-ideas] Re: Default List .get Method

2021-07-06 Thread MRAB
On 2021-07-07 01:36, Steven D'Aprano wrote: On Tue, Jul 06, 2021 at 02:54:32PM +0100, MRAB wrote: This has been discussed before, most recently on 2021-06-05, but also in August 2020, and in 2017. Hi MRAB, Thanks for looking those up, but you don't happen to have URLs do you? O

[Python-ideas] Re: Default List .get Method

2021-07-06 Thread MRAB
On 2021-07-06 02:52, Austin Graham wrote: I'm a lurker on this list and wanted to submit my own idea. It's a feature I've been wanting in Python for a while, and I have a simple implementation for it for personal use at the moment. Wanted to get feedback before I move forward. Essentially, I

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread MRAB
On 2021-06-17 00:57, Chris Angelico wrote: On Thu, Jun 17, 2021 at 9:46 AM Oliver Margetts wrote: > I'm not sure why it doesn't special-case it to "".join() One reason might be because you'd have to read the entire iterator to know if it really was only strings. So there are concerns with gen

[Python-ideas] Re: The name Ellipsis should be a constant

2021-05-31 Thread MRAB
On 2021-05-31 21:19, Serhiy Storchaka wrote: 31.05.21 22:46, Chris Angelico пише: Originally, the notation "..." could only be used inside a subscript, and anywhere else, you'd have to spell it "Ellipsis". Now that you can use "..." anywhere, would it be worth switching the repr to just be that?

[Python-ideas] Re: The name Ellipsis should be a constant

2021-05-31 Thread MRAB
On 2021-05-31 15:55, Paul Bryan wrote: If you're proposing prevention of monkey patching Ellipsis, I think you'll have to go all-in on all builtins. For example: str = int str str == int True If you rebind str to int, the repr of str will say , so you can tell that something's ha

[Python-ideas] Re: Add static variable storage in functions

2021-05-27 Thread MRAB
On 2021-05-27 21:02, Brendan Barnwell wrote: On 2021-05-27 12:33, Chris Angelico wrote: With statics, you could write it like this: def merge_shortest(things): static len=len ... Simple. Easy. Reliable. (And this usage would work with pretty much any of the defined semantics.) There'

[Python-ideas] Re: Add static variable storage in functions

2021-05-27 Thread MRAB
On 2021-05-27 17:44, Shreyan Avigyan wrote: My proposal is somewhat the sum of all of your ideas. Well I propose there should a STORE_STATIC_FAST opcode that stores a static variable. Static variable will be declared only once and will be initialized to None (statement syntax will be similar t

[Python-ideas] Re: Add static variable storage in functions

2021-05-27 Thread MRAB
On 2021-05-27 10:39, Shreyan Avigyan wrote: Well sometimes we don't want to pollute the module namespace. Two functions can have two variables with the same name but with different values that we want to be static. And this functionality already exists in Python but as a *hack*. This idea prop

[Python-ideas] Re: Introduce constants in Python (constant name binding)

2021-05-26 Thread MRAB
On 2021-05-26 13:53, Shreyan Avigyan wrote: I've already given one. Since Python is dynamically typed changing a critical variable can cause huge instability. Want a demonstration? Here we go, import sys sys.stdout = None Now what? Now how can we print anything? Isn't this a bug? There are lot

[Python-ideas] Re: dict.get_deep()

2021-05-23 Thread MRAB
On 2021-05-23 13:37, Marco Sulla wrote: I propose to add a get_deep(*args, default=_sentinel) method to dict. It can accept a single argument, that must be an iterable, or multiple arguments. The first element must be a key of the dict. If there's not a second element, the value is returned. If

[Python-ideas] Re: Add a mechanism so that multiple exceptions can be caught using `except E1, E2, E3:`

2021-05-09 Thread MRAB
On 2021-05-09 21:11, Bruce Leban wrote: On Sun, May 9, 2021 at 9:48 AM Thomas Grainger > wrote: now that python2.7 is EOL, it might be worth resurrecting this syntax as discussed in https://www.python.org/dev/peps/pep-3100/#id13

[Python-ideas] Re: Reverse polish notation

2021-04-02 Thread MRAB
On 2021-04-02 16:55, Richard Damon wrote: I think python only has 3 unary operations, + - and ~ and only + and - can also be binary operations. unary + has less uses, since it normally just passes its argument, if valid, unchanged, but can be used to validate that its argument has the right type

[Python-ideas] Re: Alternate lambda syntax

2021-02-17 Thread MRAB
On 2021-02-17 05:57, Steven D'Aprano wrote: On Wed, Feb 17, 2021 at 11:13:08AM +1300, Greg Ewing wrote: On 17/02/21 7:10 am, Steven D'Aprano wrote: >"It's Greek letter, like pi that you may remember from maths >class. In some technical computer science, the Greek L, lambda, is used >as the symbo

[Python-ideas] Re: Conditional with statements

2021-02-06 Thread MRAB
On 2021-02-07 00:57, Jonathan Crall wrote: [snip] To be clear, in the proposed syntax:     if [condition] with [obj]:         [code] Would behave exactly as:     if [condition]:         with [obj]:             [code] Is there any chance that this conditional context manager syntax mi

[Python-ideas] Re: Adding `open_text()` builtin function. (relating to PEP 597)

2021-01-24 Thread MRAB
On 2021-01-24 17:04, Chris Angelico wrote: On Mon, Jan 25, 2021 at 3:55 AM Stephen J. Turnbull wrote: Chris Angelico writes: > Right, but as long as there's only one system encoding, that's not > our problem. If you're on a Greek system and you want to decode > ISO-8859-9 text, you have to

[Python-ideas] Re: Adding `open_text()` builtin function. (relating to PEP 597)

2021-01-23 Thread MRAB
On 2021-01-24 01:14, Guido van Rossum wrote: I have definitely seen BOMs written by Notepad on Windows 10. Why can’t the future be that open() in text mode guesses the encoding? "In the face of ambiguity, refuse the temptation to guess." ___ Python-i

[Python-ideas] Re: Adding `open_text()` builtin function. (relating to PEP 597)

2021-01-23 Thread MRAB
On 2021-01-23 10:11, Chris Angelico wrote: [snip] Okay. If the goal is to make UTF-8 the default, may I request that PEP 597 say so, please? With a heading of "deprecation", it's not really clear what its actual goal is. From the sound of things - and it's still possible I'm misreading PEP 59

[Python-ideas] Re: Move semantics

2020-11-26 Thread MRAB
On 2020-11-27 02:30, Ned Batchelder wrote: On 11/26/20 9:02 PM, MRAB wrote: On 2020-11-27 01:12, Ned Batchelder wrote: On 11/26/20 6:44 AM, 3mi...@gmail.com wrote: Add something like Move type hint to typing module. It will tell the analyzer that the input parameter of the function is moved

[Python-ideas] Re: Move semantics

2020-11-26 Thread MRAB
On 2020-11-27 01:12, Ned Batchelder wrote: On 11/26/20 6:44 AM, 3mi...@gmail.com wrote: Add something like Move type hint to typing module. It will tell the analyzer that the input parameter of the function is moved and can not be used after. For example: ``` def f(d: Move[dict]) -> dict:

[Python-ideas] Re: Adding PyInstaller to the standard library

2020-11-24 Thread MRAB
On 2020-11-25 00:42, Greg Ewing wrote: On 25/11/20 12:14 pm, Chris Angelico wrote: If you want a perfectly out-of-the-box app, you're probably going to have to stick to Tkinter. Which is only bundled with Python on Windows, as far as I know. It's also bundled with Python on Raspbian. ___

[Python-ideas] Re: Dict unpacking assignment

2020-10-26 Thread MRAB
On 2020-10-26 04:56, Dennis Sweeney wrote: What if the mapping assignment were more harmonious with the pattern matching PEP? Something like this: items = {'eggs': 2, 'cheese': 3, 'spam': 1} {'eggs': eggs, 'spam': i_dont_need_to_name_this_spam, **rest} = items assert i_dont_need_

[Python-ideas] Re: Extrapolating PEP 634: The walrus was waiting for patmat all along

2020-10-23 Thread MRAB
On 2020-10-23 23:25, Valentin Berlier wrote: Pattern-matching is great. I think PEP 634 is on the right track, but it would be a waste to only use pattern-matching for choosing a branch in a match statement. Let’s look at Rust: if let [x, y] = my_array { ... } Rust "if le

[Python-ideas] Re: f-strings as assignment targets

2020-10-22 Thread MRAB
On 2020-10-22 08:50, M.-A. Lemburg wrote: On 22.10.2020 04:12, David Mertz wrote: To bring it back to a concrete idea, here's how I see things: 1. The idea of f-string-like assignment targets has little support.  Only Chris, and maybe the OP who seems to have gone away. 2. The idea of a "

[Python-ideas] Re: New feature

2020-10-13 Thread MRAB
On 2020-10-14 00:46, Steven D'Aprano wrote: On Tue, Oct 13, 2020 at 03:35:58PM -0700, Guido van Rossum wrote: Can one of the educators on the list explain why this is such a commonly required feature? I literally never feel the need to clear my screen -- but I've seen this requested quite a few

[Python-ideas] Re: Add _KB, _MB, _GB to numeric literals

2020-10-13 Thread MRAB
On 2020-10-13 06:06, malin...@163.com wrote: PEP 515 added underscore to numeric literals, it brings better readability. PEP 515 -- Underscores in Numeric Literals https://www.python.org/dev/peps/pep-0515/ Is it possible to add _KB, _MB, _GB to numeric literals, for example: 20

[Python-ideas] Re: PEP 637 and keyword only subscripts

2020-09-28 Thread MRAB
On 2020-09-28 06:51, Chris Angelico wrote: On Mon, Sep 28, 2020 at 3:33 PM Christopher Barker wrote: As for using an empty tuple, thanks Guido for laying out the logic so succinctly, and it does make it pretty simple that only the one index case is special. Nevertheless, I think most folks ex

[Python-ideas] Re: PEP 637 and keyword only subscripts

2020-09-27 Thread MRAB
On 2020-09-27 21:47, David Mertz wrote: On Sun, Sep 27, 2020 at 10:41 AM Stefano Borini mailto:stefano.bor...@gmail.com>> wrote: I kept the tuple as the accepted option, but I am personally open to NoIndex as well. I am not sure how the SC would take a non-hashable, new constant to

[Python-ideas] Re: Suggestion: annotated exceptions

2020-09-25 Thread MRAB
On 2020-09-26 01:23, Greg Ewing wrote: On 26/09/20 4:32 am, Oscar Benjamin wrote: annotations could be used to document in a statically analysable way what the "expected" exceptions are. A type checker could use those to check whether a caller is handling the *expected* exceptions But that wou

[Python-ideas] Re: PEP 637 - support for indexing with keyword arguments (Was: Re: PEP 9999 (provisional): ...)

2020-09-25 Thread MRAB
On 2020-09-25 20:36, Christopher Barker wrote: On Fri, Sep 25, 2020 at 6:05 AM Ricky Teachey > wrote: I'd like to hear more about why the empty tuple has been selected as the default index. It makes sense to me: if more than one index is passed, they are pas

[Python-ideas] Re: What about having a .get(index, default) method for arrays like we have for dicts?

2020-08-25 Thread MRAB
On 2020-08-25 13:52, Daniel. wrote: I just came across this again while implementing an parser I would like to compare stack elements as if stack[-3] == x and stack[-2] == y and stack[-1] == z if stack[-3 : ] == [x, y, z]: and somewere below elif stack[-1] == t elif stack[-1 : ] == [t]:

[Python-ideas] Re: Package kwkey and PEP 472 -- Support for indexing with keyword arguments

2020-08-07 Thread MRAB
On 2020-08-07 13:13, Jonathan Fine wrote: We are discussing a proposal to extend Python's syntax to allow     d[1, 2, a=3, b=4] We are also discussing the associated semantics. At present     d[1, 2]     d[(1, 2)] are semantically equivalent. Python behaves as though it's wrapping parenthes

  1   2   3   4   >