On 12/6/2021 1:24 AM, Christopher Barker wrote:
On Sun, Dec 5, 2021 at 10:38 AM David Mertz, Ph.D.
<david.me...@gmail.com> wrote:
I first discussed the idea of a "generalized deferred object/type"
on this list at least two years ago, probably more than three (I
haven't looked through archives lately to be sure the dates). The
idea got some vague interest, but I was too lazy, or too busy, or
whatever, to write an actual PEP or implementation.
I don’t think a full PEP is required at this point, but throughout
this discussion, the idea of a deferred object being a better solution
to this same problem has been brought up.
My sense it that some folks think if we want late-bound defaults, we
really should just do deferred objects.
But I honestly don’t get it. My idea of a deferred object would be
quite different that this, would not be a great replacement for this,
and could quite happily co-exist with this idea. Clearly I’m missing
something.
I've used this example before.
Let's assume backticks create a "deferred object" (a name I hate, but
I'll continue to use in this discussion). I realize backticks won't fly,
I'm using them deliberately so as to not argue about the syntax here.
If I want to specify a late bound dataclasses default parameter,
currently I have to say:
@dataclasses.dataclass
class A:
my_list: list = dataclasses.field(default_factory=list)
What I'd like to be able to say:
@dataclasses.dataclass
class A:
my_list: list = `[]`
In the class A, before @dataclass is called, I want A.my_list to be a
"deferred object" that I could then use when @dataclass is generating
__init__(). Exactly how and when the "deferred object" would get
evaluated is debatable, but not so important for the sake of this
discussion. Suffice it to say that it would either be explicitly or
implicitly evaluated at the start of __init__.
I think you can see that this would benefit from similar functionality
to late-bound parameters, and that if we had this more general mechanism
that late-bound parameters could use the same underlying mechanism.
Had these "deferred objects" existed when I designed dataclasses, I
would have used them instead of the clunky default_factory. PEP 671 does
not help with this use case, where a late-bound parameter isn't
specified in a function definition. I need the late-bound parameter to
be stored in an object I can refer to later.
Eric
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/MGXJ34KNQ3WIQMZJ7ETOFPPTJNT4AK3D/
Code of Conduct: http://python.org/psf/codeofconduct/