Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ability to override __slots__ with custom descriptors, especially non-data #5

Open
smarie opened this issue Jan 31, 2020 · 0 comments
Labels

Comments

@smarie
Copy link
Owner

smarie commented Jan 31, 2020

It is currently not possible for a class to define an attribute in the __slots__ (therefore generating a descriptor behind the scenes) and then to define a descriptor on that class with the same name.

However that would be desirable in certain cases. For example in the case of pyfields : in case of a NativeField (a field with no hook on set), for example obtained with field(default=1), that descriptor is implemented with a non-data descriptor overriding itself with the default value on first get, or replaced by user-provided value on first set. It could therefore easily fallback to the slot descriptor after first get or set.

from pyfields import field

class A:
    # __slots__ = ('a', )   <-- this does not work
    a = field(default=1)
# set first
o = A()
o.a = 2
# get first
o = A()
print(o.a)

See smarie/python-pyfields#19 (comment)

@smarie smarie changed the title [pyfields] ability to override __slots__ with custom descriptors, especially non-data Ability to override __slots__ with custom descriptors, especially non-data Sep 7, 2020
@smarie smarie added the pyfields label Sep 7, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant