Re: [Python-ideas] Enhancing vars()

2016-12-14 Thread Nick Coghlan
On 14 December 2016 at 10:40, Steven D'Aprano wrote: > On Wed, Dec 14, 2016 at 12:12:39AM +, Emanuel Barry wrote: >> > From Steven D'Aprano >> > Sent: Tuesday, December 13, 2016 6:49 PM >> > To: python-ideas@python.org >> > Subject: Re: [Py

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Steven D'Aprano
On Wed, Dec 14, 2016 at 12:12:39AM +, Emanuel Barry wrote: > > From Steven D'Aprano > > Sent: Tuesday, December 13, 2016 6:49 PM > > To: python-ideas@python.org > > Subject: Re: [Python-ideas] Enhancing vars() > > > > But if the object has __slots

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Emanuel Barry
> From Steven D'Aprano > Sent: Tuesday, December 13, 2016 6:49 PM > To: python-ideas@python.org > Subject: Re: [Python-ideas] Enhancing vars() > > But if the object has __slots__, with or without a __dict__, then vars > should return a proxy which direct reads and write

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Steven D'Aprano
On Tue, Dec 13, 2016 at 11:53:44AM +, Paul Moore wrote: [...] > There's obviously a speed-up from avoiding repeated getattr calls, but > is speed the key here? Not for me. > The advantage of an "enhanced vars" is more likely to be ease of > discoverability, and I'm not sure dict.fromattrs giv

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Steven D'Aprano
On Mon, Dec 12, 2016 at 10:45:39PM -0500, Alexander Belopolsky wrote: > On Mon, Dec 12, 2016 at 6:45 PM, Steven D'Aprano > wrote: > > > Proposal: enhance vars() to return a proxy to the object namespace, > > regardless of whether said namespace is __dict__ itself, or a number of > > __slots__, or

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Steven D'Aprano
On Wed, Dec 14, 2016 at 12:58:16AM +0200, Serhiy Storchaka wrote: > On 13.12.16 01:45, Steven D'Aprano wrote: > >One of the lesser-known ones is vars(obj), which should be used in place > >of obj.__dict__. > > > >Unfortunately, vars() is less useful than it might be, since not all > >objects have a

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Serhiy Storchaka
On 13.12.16 01:45, Steven D'Aprano wrote: One of the lesser-known ones is vars(obj), which should be used in place of obj.__dict__. Unfortunately, vars() is less useful than it might be, since not all objects have a __dict__. Some objects have __slots__ instead, or even both. That is considered

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Matt Gilson
> It also refers to local and global variables, as vars() is effectively > an alias for locals() if you don't pass an argument, and locals() is > effectively an alias for globals() at module level: > > https://www.getpattern.com/meetpattern> to sign up! __

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Stephen J. Turnbull
Nick Coghlan writes: > (With the lack of an underscore being due to the precedent set by > dict.fromkeys()) > > Armed with those, the "give me all the attributes from __dir__" > command would be: > > attrs = dict.from_attrs(obj, dir(obj)) A Urk

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Paul Moore
On 13 December 2016 at 11:28, Nick Coghlan wrote: > Armed with those, the "give me all the attributes from __dir__" > command would be: > > attrs = dict.from_attrs(obj, dir(obj)) Which of course can already be spelled as attrs = { attr: getattr(obj, attr) for attr in dir(obj) } There's

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Nick Coghlan
On 13 December 2016 at 20:02, Steven D'Aprano wrote: > On Tue, Dec 13, 2016 at 10:29:38AM +0100, Marco Buttu wrote: > >> +1. Would it be possible in the future (Py4?) to change the name `vars` >> to a more meaningful name? Maybe `namespace`, or something more appropriate. > > I'm not keen on the n

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Steven D'Aprano
On Tue, Dec 13, 2016 at 10:29:38AM +0100, Marco Buttu wrote: > +1. Would it be possible in the future (Py4?) to change the name `vars` > to a more meaningful name? Maybe `namespace`, or something more appropriate. I'm not keen on the name vars() either, but it does make a certain sense: short f

Re: [Python-ideas] Enhancing vars()

2016-12-13 Thread Marco Buttu
On 13/12/2016 00:45, Steven D'Aprano wrote: In general, directly accessing dunders is a bit of a code smell. (I exclude writing dunder methods in your classes, of course.) There's usually a built-in or similar to do the job for you, e.g. instead of iterator.__next__() we should use next(iterator

Re: [Python-ideas] Enhancing vars()

2016-12-12 Thread Steve Dower
Top-posted from my Windows Phone -Original Message- From: "Alexander Belopolsky" Sent: ‎12/‎12/‎2016 19:47 To: "Steven D'Aprano" Cc: "python-ideas" Subject: Re: [Python-ideas] Enhancing vars() On Mon, Dec 12, 2016 at 6:45 PM, Steven D'Apra

Re: [Python-ideas] Enhancing vars()

2016-12-12 Thread Alexander Belopolsky
On Mon, Dec 12, 2016 at 6:45 PM, Steven D'Aprano wrote: > Proposal: enhance vars() to return a proxy to the object namespace, > regardless of whether said namespace is __dict__ itself, or a number of > __slots__, or both. > How do you propose dealing with classes defined in C? Their objects don

Re: [Python-ideas] Enhancing vars()

2016-12-12 Thread Ethan Furman
On 12/12/2016 03:45 PM, Steven D'Aprano wrote: Proposal: enhance vars() to return a proxy to the object namespace, regardless of whether said namespace is __dict__ itself, or a number of __slots__, or both. +1 -- ~Ethan~ ___ Python-ideas mailing lis

Re: [Python-ideas] Enhancing vars()

2016-12-12 Thread Terry Reedy
On 12/12/2016 6:45 PM, Steven D'Aprano wrote: In general, directly accessing dunders is a bit of a code smell. (I exclude writing dunder methods in your classes, of course.) There's usually a built-in or similar to do the job for you, e.g. instead of iterator.__next__() we should use next(iterato

[Python-ideas] Enhancing vars()

2016-12-12 Thread Steven D'Aprano
In general, directly accessing dunders is a bit of a code smell. (I exclude writing dunder methods in your classes, of course.) There's usually a built-in or similar to do the job for you, e.g. instead of iterator.__next__() we should use next(iterator). One of the lesser-known ones is vars(obj