On 12/11/2021 12:11 PM, Chris Angelico wrote:
On Sun, Dec 12, 2021 at 4:06 AM Christopher Barker <python...@gmail.com> wrote:
Here’s a new (not well thought out) idea:

@dataclasses.dataclass
class A:
     Input_list: list
     length: int => len(input_list)

So length gets set to a “late bound default expression” that Is an actual 
value. It would have to store the expression itself, probably as a string, and 
it would get evaluated in the namespace of the function, when the function was 
called. So the generated __init__ would be the same as:

def __init__(self, input_list, length=>len(input_list):
If I'm not mistaken, dataclasses generate __init__ using exec anyway,
so this would work with a plain string.

The current version does, but I've produced previous versions that don't. They built up the AST themselves and use eval() on that.

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

Reply via email to