[Python-ideas] Adding a .find() method to list

2022-05-07 Thread python-ideas
In its current implementation, the list type does not provide a simple and straightforward way to retrieve one of its elements that fits a certain criteria. If you had to get the user where user['id'] == 2 from this list of users, for example, how would you do it? users = [ {'id':

[Python-ideas] Re: Adding a .find() method to list

2022-05-07 Thread Jonathan Fine
Hi Lucas.rs You wrote: > In its current implementation, the list type does not provide a simple and > straightforward way to retrieve one of its elements that fits a certain > criteria. > Thank you. You've asked a good question. I hope my answer will be helpful. Here's my preferred solution,

[Python-ideas] Re: Auto assignment of attributes

2022-05-07 Thread Christopher Barker
On Sat, May 7, 2022 at 6:28 AM Chris Angelico wrote: > > > What would this do? > > > > > > def __init__(self, spam.x, eggs.y): pass > > > > > > How about this? > > > > > > def __init__(self, x, x.y): pass > > IMO, both of those should be errors. This syntax only makes much > >

[Python-ideas] Re: Adding a .find() method to list

2022-05-07 Thread Jonathan Fine
Hi Paul You wrote: > You [the original poster] seem to want a function[to express the > criteria], but it's not obvious to me why you need that. A function has several advantages. 1. It can be reused. 2. It can be tested (a special case of 1). 3. It can be passed as a parameter (a special

[Python-ideas] Re: Auto assignment of attributes

2022-05-07 Thread Ethan Furman
On 5/7/22 06:24, Chris Angelico wrote: > On Sat, 7 May 2022 at 23:15, Stephen J. Turnbull wrote: >> >> def foo(self, x, y): >> x.y = y >> >> is not a pattern I can recall ever seeing > > I'd define it very simply. For positional args, these should be > exactly equivalent: > > def

[Python-ideas] Re: Adding a .find() method to list

2022-05-07 Thread Paul Moore
On Sat, 7 May 2022 at 16:42, wrote: > > In its current implementation, the list type does not provide a simple and > straightforward way to retrieve one of its elements that fits a certain > criteria. > > If you had to get the user where user['id'] == 2 from this list of users, for > example,

[Python-ideas] Re: Adding a .find() method to list

2022-05-07 Thread Joao S. O. Bueno
Still - the "filter" call is almost as simple as it can get for a generic enough way to do what you are requesting. There is some boiler plate needed around it if you want an actual eager result or a default value, if no match is found, that is true - but still, given a list like On Sat, May 7,

[Python-ideas] Re: Adding a .find() method to list

2022-05-07 Thread Christopher Barker
On Sat, May 7, 2022 at 9:15 AM Jonathan Fine wrote: > Here's my preferred solution, using Python builtins: > >>> users = [ > ... {'id': 1,'name': 'john'}, > ... {'id': 2, 'name': 'anna'}, > ... {'id': 3, 'name': 'bruce'}, > ... ] > >>> func = (lambda user: user['id'] ==

[Python-ideas] Re: Auto assignment of attributes

2022-05-07 Thread Stephen J. Turnbull
Steven D'Aprano writes: > What would this do? > > def __init__(self, spam.x, eggs.y): pass > > How about this? > > def __init__(self, x, x.y): pass IMO, both of those should be errors. This syntax only makes much sense for the first formal argument of a method definition,

[Python-ideas] Re: Auto assignment of attributes

2022-05-07 Thread Chris Angelico
On Sat, 7 May 2022 at 23:15, Stephen J. Turnbull wrote: > > Steven D'Aprano writes: > > > What would this do? > > > > def __init__(self, spam.x, eggs.y): pass > > > > How about this? > > > > def __init__(self, x, x.y): pass > > IMO, both of those should be errors. This syntax only

[Python-ideas] Re: Auto assignment of attributes

2022-05-07 Thread Chris Angelico
On Sun, 8 May 2022 at 10:23, Steven D'Aprano wrote: > > On Sat, May 07, 2022 at 11:38:19AM -0700, Ethan Furman wrote: > > > > I'd define it very simply. For positional args, these should be > > > exactly equivalent: > > > > > > def func(self, x, x.y): > > > ... > > > > > > def func(*args): >

[Python-ideas] Re: Auto assignment of attributes

2022-05-07 Thread Steven D'Aprano
On Sat, May 07, 2022 at 11:38:19AM -0700, Ethan Furman wrote: > > I'd define it very simply. For positional args, these should be > > exactly equivalent: > > > > def func(self, x, x.y): > > ... > > > > def func(*args): > > self, x, x.y = args > > ... > > Simple or not, I don't

[Python-ideas] Re: Auto assignment of attributes

2022-05-07 Thread Steven D'Aprano
On Sun, May 08, 2022 at 11:02:22AM +1000, Chris Angelico wrote: > On Sun, 8 May 2022 at 10:23, Steven D'Aprano wrote: > > Outside of that narrow example of auto-assignment of attributes, can > > anyone think of a use-case for this? > > > > Honestly, I don't know of any. But in response to the

[Python-ideas] Re: Adding a .find() method to list

2022-05-07 Thread Steven D'Aprano
On Sat, May 07, 2022 at 04:01:34AM -, python-id...@lucas.rs wrote: > If you had to get the user where user['id'] == 2 from this list of > users, for example, how would you do it? > > users = [ > {'id': 1,'name': 'john'}, > {'id': 2, 'name': 'anna'}, > {'id': 3, 'name': 'bruce'},