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 -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/4JN76TMBGA3YDN5445OR2S7OT32JH2XO/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/5NZOVZQQZMR2B75MKA3P5TLIURYLNP23/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to