This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Thomas701
Recipients Michael Robellard, Thomas701, UnHumbleBen, eric.smith, iivanyuk, juanpa.arrivillaga
Date 2021-10-21.17:09:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1634836160.54.0.88874472772.issue39247@roundup.psfhosted.org>
In-reply-to
Content
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 ?
History
Date User Action Args
2021-10-21 17:09:20Thomas701setrecipients: + Thomas701, eric.smith, Michael Robellard, juanpa.arrivillaga, iivanyuk, UnHumbleBen
2021-10-21 17:09:20Thomas701setmessageid: <1634836160.54.0.88874472772.issue39247@roundup.psfhosted.org>
2021-10-21 17:09:20Thomas701linkissue39247 messages
2021-10-21 17:09:20Thomas701create