A couple of thoughts: Does attrs have anything similar? Is this just a subset of a more general-purpose idea to have a different attribute name than the __init__ parameter name? What should repr show? The __init__ parameter name? Or the attribute name? I'd guess the __init__ parameter name. I'm not crazy about adding more uses of runtime type inspection, but I guess that ship has sailed with InitVar and ClassVar. Eric On 8/23/2020 5:36 PM, zachb1996--- via Python-ideas wrote:
I have a proposal for an addition to the dataclasses module that I think would make it easier to use private and name mangled variables. One of the benefits of the dataclass decorator is that it helps lessen the amount of code when you just need a simple constructor. A common pattern in python that isn't addressed by the current implementation is the pattern
```python class MyClass:
def __init__(self, a: str, b: str): self._a = a self.__b = b ```
right now the only straightforward way of doing this with a dataclass is
```python @dataclass class MyClass: a: InitVar[str] b: InitVar[str]
def __post_init__(self, a, b): self._a = a self.__b = b ```
My proposal would be to add two types to the dataclasses module, one called PrivateVar and one called MangledVar. These would add The above pattern (without the post_init ) using the syntax,
```python @dataclass class MyClass: a: PrivateVar[str] b: MangledVar[str] ```
I've already looked into what would need to be added to dataclasses to implement these and it's just a few lines of code since the class implementation would be very similar to InitVar. Is this something that other see the possibility of being added to python? (Also, I know name mangling isn't super common but I think it may as well be added alongside a private variable implementation)) _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/4JN76T... Code of Conduct: http://python.org/psf/codeofconduct/