[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Marc-Andre Lemburg
Perhaps more people need to be made aware of the __main__.py package module feature we have in Python: https://docs.python.org/3/library/__main__.html Instead of just a single main() function, you get a whole module to play with :-) -- Marc-Andre Lemburg eGenix.com Professional Python Services

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 08:33:59PM -0600, Finn Mason wrote: > I've found that the `if __name__ == "__main__"` idiom is unintuitive and > feels unnecessary, The Python community thinks nothing of writing code with metaclasses, functions and classes as first-class citizens, recursion, parallisatio

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Christopher Barker
> plenty of people write Python scripts with no main() function and no > need to test for `if __name__ == __main__`. > > And there is nothing wrong with that. Indeed, I think there is something “right” with that. The if __name__ block is only required for a Python file to be both a module and a

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Steven D'Aprano
On Sun, Oct 03, 2021 at 01:26:06AM -, Debashish Palit wrote: > The floatify solution is a bit involved - using map inside a list > comprehension. Usually list comprehensions replace the usage of map. > Also floatify requires 5 extra lines of code (putting `return` on the > same line is not

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 09:22:04AM +, Zbigniew Jędrzejewski-Szmek wrote: > A nice bonus is that this scheme is very close to main() in C/C++/Java > and other compiled languages, so users coming in from those languages > will understand this without further explanation. I don't think they will

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 10:17:12AM -0700, Paul Bryan wrote: > As I understand, the arguments for: > - let's get rid of boilerplate, that many (esp. beginners) may not > understand The proposal doesn't get rid of boilerplate, it just changes it from one form to another. The current idiom uses an

[Python-ideas] Re: More efficient list copying

2021-10-02 Thread Christopher Barker
> See the Stackoverflow post I linked to at the start of my post. > > > https://stackoverflow.com/questions/56966429/getting-pairs-of-one-item-and-the-rest-over-a-python-list I’m confused— that seems to be a SO post related to another ongoing thread…

[Python-ideas] Re: Better exception handling hygiene

2021-10-02 Thread Soni L.
On 2021-10-01 8:26 a.m., Steven D'Aprano wrote: > I wish your proposals and ideas would be more *precise* in their > specifications. This is not the first time that I have found it very > hard to work out precisely what you are suggesting and what you are not. > > Thanks for insisting on cont

[Python-ideas] Feature request enumerate_with_rest or enumerate with skip or filter callback

2021-10-02 Thread Stephen J. Turnbull
Laurent Lyaudet writes: > Hello, > > This is a very simple feature request that does not break anything but > I don't know if you may find it interesting. > It would be nice to have a function or method of list objects that does this > : > - First idea : > def enumerate_with_rest(my_list)

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

2021-10-02 Thread Abdulla Al Kathiri
Then use it with the normal expression lambda: people.sort(key=p => (p.salary, p.name, p.id)). You don’t need lambda set for that. If you want to use it, it will be like the following: people.sort(key=p => {(p.salary, p.name, p.id)}). The tuple expression is the last item of the set, so the tu

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 05:13:01PM +0100, Alex Waygood wrote: > I disagree that "it teaches a lot about how Python works" is a good > reason to keep things the way they are. If you applied this principle > more broadly, it would seem to be an argument in favour of complexity > in most situation

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread João Bernardo
Maybe @lambda x: x if __name__ == "__main__" and x() else x so you can call the main function again, if you want. On Sat, Oct 2, 2021 at 6:19 PM Anthony Baire wrote: > why not evaluate the condition directly in the decorator, python 3.9 > allows that ;-p > > @lambda x: __name__ == "__main__

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Finn Mason
I've found that the `if __name__ == "__main__"` idiom is unintuitive and feels unnecessary, and I'd like a cleaner way to write it. However, I don't like the idea of functions being "magically called." One of the things I don't like about C/C++ and so many other languages is the presence of a `main

[Python-ideas] Re: More efficient list copying

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 07:57:48AM -0700, Guido van Rossum wrote: > No, it would also have to increment the reference count of each item (since > blist owns a reference to each). That's what makes this slow. Ahaha, of course, I forgot about the ref counting. > > There are lots of other variants

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 01:06:46PM -, Debashish Palit wrote: [...] > Consider a number radix conversion software. It needs to accept the > input number as a string (say '15A' in base 12). When we provide it a > base 10 number, it would also be a string. Why a string? That's an unusual API.

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Debashish Palit
The floatify solution is a bit involved - using map inside a list comprehension. Usually list comprehensions replace the usage of map. Also floatify requires 5 extra lines of code (putting `return` on the same line is not a best practice). Why not do it with one simple line of code ? Debashish

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

2021-10-02 Thread Brendan Barnwell
On 2021-09-29 10:11, MRAB wrote: I'd prefer something like "x -> x" "x => x" as an equivalent to "lambda x: x": I continue to find all such syntaxes less readable even than lambda. The idea of using a hyphen and a greater-than sign to "draw" an arrow doesn't sit well with me. The only pro

[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 Chris Angelico
On Sun, Oct 3, 2021 at 9:04 AM Abdulla Al Kathiri wrote: > > Yeah empty parentheses for parameters-less function is the clear obvious way. > Optional parenthesis for single parameter function is a wise choice. In fact, > I read C# lambdas and they made really great design choices. I particularl

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

2021-10-02 Thread Abdulla Al Kathiri
Yeah empty parentheses for parameters-less function is the clear obvious way. Optional parenthesis for single parameter function is a wise choice. In fact, I read C# lambdas and they made really great design choices. I particularly like the statements lambda. How about doing it in Python with t

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Anthony Baire
why not evaluate the condition directly in the decorator, python 3.9 allows that ;-p @lambda x: __name__ == "__main__" and x() def main():     ... On 01/10/2021 21:35, Jonathan Crall wrote: I was curious if / what sort of proposals have been considered for simplifying the pattern: ``` def m

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

2021-10-02 Thread Barry Scott
> On 2 Oct 2021, at 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. > While you may not see the point at first glance, let me try to reason such

[Python-ideas] Re: Feature request enumerate_with_rest or enumerate with skip or filter callback

2021-10-02 Thread Laurent Lyaudet
Le sam. 2 oct. 2021 à 15:54, a écrit : > Date: Sat, 2 Oct 2021 12:45:33 +0200 > From: Laurent Lyaudet > Subject: [Python-ideas] Feature request enumerate_with_rest or > enumerate with skip or filter callback > To: python-ideas@python.org > Message-ID: > > Content-Type: text/plain

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Chris Angelico
On Sun, Oct 3, 2021 at 4:18 AM Paul Bryan wrote: > > Thanks for finding that. > > While I don't feel strongly one way or the other, I do think the discussion > is worthwhile. > > As I understand, the arguments for: > - let's get rid of boilerplate, that many (esp. beginners) may not understand I

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Barry Scott
> On 2 Oct 2021, at 18:17, Paul Bryan wrote: > > Thanks for finding that. > > While I don't feel strongly one way or the other, I do think the discussion > is worthwhile. > > As I understand, the arguments for: > - let's get rid of boilerplate, that many (esp. beginners) may not understand

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Paul Bryan
Thanks for finding that. While I don't feel strongly one way or the other, I do think the discussion is worthwhile. As I understand, the arguments for: - let's get rid of boilerplate, that many (esp. beginners) may not understand - you could add command line arguments and return values in a more

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

2021-10-02 Thread MRAB
On 2021-10-02 08:59, Abdulla Al Kathiri wrote: Let’s say I want to write a lambda function with no arguments that’s connected to a button in GUI coding, will blabla.connect(()=>print(“clicked”)) be used or will blabla.connect(=>print(“clicked”)) be used? In the case of C#, the parentheses are

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Chris Angelico
On Sun, Oct 3, 2021 at 1:55 AM Debashish Palit wrote: > > Ok it is a 3 way flag, but the code is not awkward. 'c' is just a name I > chose - it does not mean anything. > > The check for a valid number can be used on a list of strings - > > [float(s) for s in lst if s.isfloat() is not None] > > No

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Eric V. Smith
See the rejected PEP 299 for a previous version of this idea: https://www.python.org/dev/peps/pep-0299/ And a discussion on python-dev: https://mail.python.org/pipermail/python-dev/2006-March/062951.html Eric On 10/1/2021 3:38 PM, Paul Bryan wrote: How about the following? def __main__():

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Alex Waygood
I disagree that "it teaches a lot about how Python works" is a good reason to keep things the way they are. If you applied this principle more broadly, it would seem to be an argument in favour of complexity in most situations, that would imply we should keep syntactic sugar to a bare minimum at

[Python-ideas] Re: Reusing more builtins for type-hinting

2021-10-02 Thread Ricky Teachey
On Sat, Oct 2, 2021, 10:40 AM Ricky Teachey wrote: > Just spitballing but how about any.obj as an alias for typing.Any? It > suffers from the same problem as using naked any- which is that any doesn't > really have anything to do with type hinting... but it doesn't suffer from > the problem of ha

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Debashish Palit
Ok it is a 3 way flag, but the code is not awkward. 'c' is just a name I chose - it does not mean anything. The check for a valid number can be used on a list of strings - [float(s) for s in lst if s.isfloat() is not None] No 3 way flag here. Difficult to do with EAFP. _

[Python-ideas] Re: More efficient list copying

2021-10-02 Thread David Mertz, Ph.D.
On Sat, Oct 2, 2021, 10:58 AM Guido van Rossum wrote: > Are you actually observing that people are doing this with regular lists? > Don't people working with Big Data usually use Pandas, which is built on > NumPy arrays and custom data structures? > Basically, Guido is right. Big data lives in N

[Python-ideas] Re: More efficient list copying

2021-10-02 Thread Guido van Rossum
On Sat, Oct 2, 2021 at 7:42 AM Steven D'Aprano wrote: > This half-baked idea is inspired by this thread here: > > > https://mail.python.org/archives/list/python-ideas@python.org/message/5LGWV3YLCNBVSL4QHQKJ7RPNTMWOALQA/ > > which in turn was inspired by this Stackoverflow post: > > > https://stac

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 01:21:46PM -, Debashish Palit wrote: > Steven D'Aprano wrote: > > On Sat, Oct 02, 2021 at 02:53:03AM -, Debashish Palit wrote: > > > There is no need of a three_way_flag - just use a conditional > > > expression instead of an if-elif-else block, > Using a condition

[Python-ideas] More efficient list copying

2021-10-02 Thread Steven D'Aprano
This half-baked idea is inspired by this thread here: https://mail.python.org/archives/list/python-ideas@python.org/message/5LGWV3YLCNBVSL4QHQKJ7RPNTMWOALQA/ which in turn was inspired by this Stackoverflow post: https://stackoverflow.com/questions/56966429/getting-pairs-of-one-item-and-the-rest

[Python-ideas] Re: Reusing more builtins for type-hinting

2021-10-02 Thread Ricky Teachey
Just spitballing but how about any.obj as an alias for typing.Any? It suffers from the same problem as using naked any- which is that any doesn't really have anything to do with type hinting... but it doesn't suffer from the problem of having to convert any to a type. It would just grow a class att

[Python-ideas] Re: Reusing more builtins for type-hinting

2021-10-02 Thread Serhiy Storchaka
02.10.21 17:10, Todd пише: > Is there a reason we can't use "Object" and make "Any" just an alias for > "Object"? If you mean "object", then the reason is that "object" has no methods and attributes (besides few dunder methods like __repr__ and __reduce__), while "Any" has all methods and attribut

[Python-ideas] Re: pathlib update idea: iterable Path that returns subpaths

2021-10-02 Thread Serhiy Storchaka
02.10.21 15:44, Paul Moore пише: > The counter-argument is "there should be one obvious way" - we > definitely don't only have *one* way, at the moment, but none of them > are "obvious". My big problem is that I don't think that making Path > instances iterable is "obvious", either. What if the pat

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Debashish Palit
> As I have mentioned above, EAFP is fine but you have to apply it twice. > str.float accomplishes the same with 1 line of code. > num = float(s) if s.float() else int(s) There is a typo in my reply as quoted: it should be .isfloat() and not .float() . ___

[Python-ideas] Re: Reusing more builtins for type-hinting

2021-10-02 Thread Todd
Is there a reason we can't use "Object" and make "Any" just an alias for "Object"? On Fri, Oct 1, 2021, 10:47 Christopher Barker wrote: > > > The fact that the built in “any” is not a type is not an implementation > detail. any() and typing.Any are completely different things/concepts. They > ju

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 01:15:28AM -0400, Jonathan Crall wrote: > On Sat, Oct 2, 2021, 12:05 AM Chris Angelico wrote: > > But it's ONLY necessary when a single Python file is used as both a > > script and a module. Bear that in mind. > > That's effectively every module I write. I don't think th

[Python-ideas] Re: Feature request enumerate_with_rest or enumerate with skip or filter callback

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 12:45:33PM +0200, Laurent Lyaudet wrote: > def enumerate_with_rest(my_list): > for i, item in enumerate(my_list): > yield i, item, my_list[:i] + my_list[i + 1:] So if you called it on the list ['a', 'b', 'c', 'd'] it would yield: 0, 'a', ['b', 'c', 'd']

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Debashish Palit
Steven D'Aprano wrote: > On Sat, Oct 02, 2021 at 02:53:03AM -, Debashish Palit wrote: > > There is no need of a three_way_flag - just use a conditional > > expression instead of an if-elif-else block, > > Of course you need a three way flag if your function returns a three way > flag. It retu

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Debashish Palit
Steven D'Aprano wrote: > On Sat, Oct 02, 2021 at 02:17:32AM -, Debashish Palit wrote: > > The method need not take care of every variation. Currently there is > > no method to check for a float in a string even without formatting. > > Right, because no method is needed. If you want to convert

[Python-ideas] Re: pathlib update idea: iterable Path that returns subpaths

2021-10-02 Thread Paul Moore
On Sat, 2 Oct 2021 at 13:22, Aaron Stacy wrote: > > I’d like to propose an enhancement to the wonderful pathlib module: make Path > instances iterable, where the iterator returns subpaths. > > This would be functionally very similar to Path(some_directory).rglob(‘*’). ... which is of course the

[Python-ideas] Allow regex group name redefinitions

2021-10-02 Thread vencik
Hello everybody, I've got a suggestion for the std. re module developers: to consider allowing match group name redefinitions, especially in alternatives. While you may not see the point at first glance, let me try to reason such a thing using a real-world example from my practice: Imagine a co

[Python-ideas] pathlib update idea: iterable Path that returns subpaths

2021-10-02 Thread Aaron Stacy
I’d like to propose an enhancement to the wonderful pathlib module: make Path instances iterable, where the iterator returns subpaths. This would be functionally very similar to Path(some_directory).rglob(‘*’). Why not just use rglob(‘*’)? While globs are an extremely useful shorthand, in my exp

[Python-ideas] Feature request enumerate_with_rest or enumerate with skip or filter callback

2021-10-02 Thread Laurent Lyaudet
Hello, This is a very simple feature request that does not break anything but I don't know if you may find it interesting. It would be nice to have a function or method of list objects that does this : - First idea : def enumerate_with_rest(my_list): for i, item in enumerate(my_list):

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 02:17:32AM -, Debashish Palit wrote: > The method need not take care of every variation. Currently there is > no method to check for a float in a string even without formatting. Right, because no method is needed. If you want to convert something to a float (whether

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 02:53:03AM -, Debashish Palit wrote: > There is no need of a three_way_flag - just use a conditional > expression instead of an if-elif-else block, Of course you need a three way flag if your function returns a three way flag. It returns False for ints, True for floa

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Abdur-Rahmaan Janhangeer
Greetings list, I am -1 on this proposition. I can relate that if name == main is very confusing to teach. However, it teaches a lot about how Python works. If you know Python it is very clear. So if it's confusing, you wrongly taught Python (I myself was in this situation) The simplification id

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Zbigniew Jędrzejewski-Szmek
On Fri, Oct 01, 2021 at 03:40:54PM -0400, Jonathan Crall wrote: > That would also make a lot of sense. That way has fewer characters without > sacrificing clarity, which is a +1 in my book. > > On Fri, Oct 1, 2021 at 3:38 PM Paul Bryan wrote: > > > How about the following? > > > > def __main__()

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Zbigniew Jędrzejewski-Szmek
On Fri, Oct 01, 2021 at 11:59:33PM -0400, Jonathan Crall wrote: > It's true that it's just a two-line pattern, and not all simplifications of > two line patterns make it into the python standard. But this is an > extremely common two line pattern. One that's often written less > efficiently than it

[Python-ideas] Re: Reusing more builtins for type-hinting

2021-10-02 Thread Serhiy Storchaka
01.10.21 15:01, Matt del Valle пише: > `any` (for use  instead of `typing.Any`) > `callable` (for use instead of `typing.Callable`) The problem is than any() and callable() are not types, but functions, whose names are accidentally similar to names of some typing types. If the name of callable() w

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Serhiy Storchaka
01.10.21 22:35, Jonathan Crall пише: > My motivation for writing this suggestion is in an attempt to stop a > common anti-pattern, where instead of defining a `main` function (or a > function by any other name) an simply calling that by adding the above > two lines, a lot of Python users I work wit

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

2021-10-02 Thread Abdulla Al Kathiri
Let’s say I want to write a lambda function with no arguments that’s connected to a button in GUI coding, will blabla.connect(()=>print(“clicked”)) be used or will blabla.connect(=>print(“clicked”)) be used? Sent from my iPhone > On 30 Sep 2021, at 7:53 PM, MRAB wrote: > > On 2021-09-30 07:

[Python-ideas] Re: Syntax Sugar for __name__ == "__main__" boilerplate?

2021-10-02 Thread Abdulla Al Kathiri
def __main__() at the end of the file will be something nice to have. It’s much easier to explain to new students than if __name__ == __main__: main(). Can we import this new __main__ function from another file and use it in another file? Maybe it should be disallowed to remove confusion and con