New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't enable GC for classes that don't add new fields #67914
Comments
As far as I can tell, if a new class does not add any new fields, and its base class doesn't use GC, there's no reason to enable GC for the new class. |
>>> class UserIntSlots(int):
... __slots__ = ()
... def __repr__(s): return '5'
...
>>> (4).__class__ = UserIntSlots
>>> 2+2
5
>>> type(2+2)
<class '__main__.UserIntSlots'> It looks weird. |
Agreed, but this is not new. This works without my change: >>> class Tuple(tuple):
... __slots__ = ()
... def __repr__(self): return 'Imma tuple!'
...
>>> ().__class__ = Tuple
>>> ()
Imma tuple! |
I wasn't aware of that :-o |
May be we should forbid assigning the __class__ attribute of basic builtin |
Actually, this is rather new -- new in 3.5. The check was relaxed in bpo-22986: https://hg.python.org/cpython/rev/c0d25de5919e |
New changeset a60b7945ef87 by Antoine Pitrou in branch 'default': |
I've pushed the patch, thank you! |
Thank you! Benjamin, Nathaniel, any opinion if we should restrict reassigning __class__ for types like tuple, int and str, where some/many instances are cached? |
Yes, it probably would be a good idea to disallow assigning __class__ for immutable types. |
Agreed. There's a small problem with that, as far as I know. Nothing on type declares that it is immutable. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: