Thomas <[email protected]> added the comment:
Thinking a little more about this, maybe a different solution would be to have
default values be installed at the class level by default without being
overwritten in the init, as is the case today. default_factory should keep
being set in the init as is the case today.
With this approach:
@dataclass
class Foo:
bar = field(default=4)
# assigns 4 to Foo.bar but not to foo.bar (bonus: __init__ will be faster)
bar = field(default=some_descriptor)
# assigns some_descriptor to Foo.bar, so Foo().bar does a __get__ on the
descriptor
bar = field(default_factory=SomeDescriptor)
# assigns a new SomeDescriptor instance to every instance of Foo
bar = field(default_factory=lambda: some_descriptor)
# assigns the same descriptor object to every instance of Foo
I don't think this change would break a lot of existing code as the attribute
overwrite that happens at the instance level in the __init__ is essentially an
implementation detail. It also seems this would solve the current problem and
allow for a cleaner way to assign a descriptor object as a default value. Am I
not seeing some obvious problem here ?
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue39247>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com