On 12/6/2021 1:24 AM, Christopher Barker wrote:
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.
And in case I wasn't clear: to get the late-bound parameter functionality using this syntax, you'd use:
def foo(my_list = `[]`):
That's why I think we should have a larger concept that just
late-bound parameters: I think there's a general concept here that
can be extended beyond parameters. And that's why I thing not
restricting it to a function-definition-only syntax is important:
we should produce a syntax that can be used in more places than
just functions. This is why I think we need to decide on this
larger scope before accepting the narrow function-definition-only
syntax: if we decide to add "deferred objects" later, we'd have
two ways to specify late-bound parameters [0].
Eric
[0]: Or arguments, I can never remember which is which: someone needs to invent a memorable mnemonic device.
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