[issue46132] Consider adding __slots__ to enums?

2021-12-19 Thread Ethan Furman


Ethan Furman  added the comment:

Some testing reveals that `__slots__` is not a good option for Enum -- it makes 
it impossible to mix in in other types such as `int`.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46132] Consider adding __slots__ to enums?

2021-12-19 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

> In which case: consider this a feature request to 
> consider adding __slots__ ...

A few thoughts:
* Enumerations tend to be small, so a space savings likely isn't relevant.
* In Python 3.11, the speed advantage of slots is now much smaller.
* The code for Enum is already complex and has a lot of magic.  Adding slots to 
the equation may make it even harder to reason about the internals.
* It not even clear that __slots__ would or could play nice with existing code. 
 Even if it does, it may tie our hands for other avenues of development such as 
a custom a __getattribute__ or use of @cached_property.

Please consider a more use case driven development approach to working on the 
core.  We have no known problems with Enum at this point that need to be 
solved.  A general notion that __slots__ should be used in more places is a 
reasonable topic for python-ideas.  The tracker is more suitable for targeted 
proposals like, "my company needs enums to work with tool x but it won't unless 
slot support is added", "my real-world app must support enormous enumerations 
that lead to memory problems unless slots are added", "i tried adding slots to 
Enum and found it was easy, didn't cause problems, and sped-up common cases by 
22%".

I mostly agree with Christian except that limiting attribute access is a 
legitimate reason to use slots.  However, in the case of Enums that isn't a 
known issue.  Also, it is something that would need to done when a tool is 
first released.  Generally, we can't add it afterwards because that would be a 
breaking change (some applications may be legitimately adding additional 
attributes).

For now, I'll close this.  If Ethan thinks there is some fruit on this tree, he 
can reopen it.  Also if a specific real world use case arises, it can be 
reopened.

--
nosy: +rhettinger
resolution:  -> rejected
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46132] Consider adding __slots__ to enums?

2021-12-19 Thread Alex Waygood

Alex Waygood  added the comment:

Ah, of course, I'm an idiot — I forgot that a class could not prevent the 
creation of __dict__ unless the parent class also had __slots__. Thanks, 
Christian.

In which case: consider this a feature request to consider adding __slots__ to 
enum.Enum: for faster attribute access, lower memory footprint, and the ability 
to define __slots__ in custom Enum classes.

--
title: Attempting to create an enum with slots silently fails -> Consider 
adding __slots__ to enums?
type: behavior -> enhancement
versions:  -Python 3.10, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com