dataclasses use Field objects that can be created automatically, but also you can specify them if you need to do something special. And one of the special things you can do is set a default constructor -- I'm sure that could be extended to support early bound defaults.
-CHB On Thu, Nov 25, 2021 at 11:40 PM Chris Angelico <ros...@gmail.com> wrote: > 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/ > -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
_______________________________________________ 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/KTBG6LBRD5NPKV2JK7BGO7FG34INNP5J/ Code of Conduct: http://python.org/psf/codeofconduct/