[Python-ideas] Enhancing vars()

Steven D'Aprano steve at pearwood.info
Tue Dec 13 18:54:10 EST 2016


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 <steve at pearwood.info>
> 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't
> have __slots__.

I don't see any clean way to do so. Maybe we should have a convention 
that such objects provide a __slots__ attribute listing public 
attributes, but I'm not too concerned. Let vars(weird_c_object) raise 
TypeError, just as it does now.


> One possibility is to use __dir__ or dir(), but those can return anything
> and in the past developers
> were encouraged to put only "useful" attributes in __dir__.

Indeed.



-- 
Steve


More information about the Python-ideas mailing list