On Fri, Nov 26, 2021 at 6:22 PM Abdulla Al Kathiri
<alkathiri.abdu...@gmail.com> wrote:
>
> Chris,
>
> Will we able to use late-bound arguments in dataclass when it’s creating the 
> __init__ function?
>
> @dataclass
> class C:
>     x: int
>     y: int
>     ls: list[int] => [x, y]
>

With that syntax, no, because there's no object that can be stored in
an annotation dictionary that would represent the code construct to
create that effect.

But the __init__ function is constructed with exec(), and that means
that, in theory, dataclasses._field_init could be enhanced to have
some way to indicate this - or, possibly, to *always* use late-bound
defaults, since it appears to use sentinels for everything.

I don't know enough about the workings of dataclasses.dataclass to be
able to say for sure, but a cursory glance does suggest that, in some
way, this should be possible. It may require stringifying the default
though.

ls: list[int] = "[x, y]"

ChrisA
_______________________________________________
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/YVLHVQK27TN6M5OKZKXJGUEZX47BRHGK/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to