[Python-ideas] Re: Enhancement: Adding support for private and name mangled type hints in dataclasses module

2020-08-24 Thread zachb1996--- via Python-ideas
While I agree that this is a subset of the idea of having a different attribute 
name, I think this naming convention is very common, and is even referenced in 
the python documentation. Which is why I think it would warrant being built in 
as opposed to using an InitVar.

So the way attrs deals with private variables is actually even less ideal, 
because it pretends that the convention doesn't even exists since in python the 
variables aren't ever truly private, you can see that 
[here](https://www.attrs.org/en/stable/init.html#private-attributes).

As for repr, I think it may actually be beneficial for these to be excluded, 
because if we're truly emulating private variables, a user shouldn't really 
know what they are without going into the code, although of course since it's 
python they could be fairly easily accessed through inspection and the dict 
attribute. 

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


[Python-ideas] Enhancement: Adding support for private and name mangled type hints in dataclasses module

2020-08-23 Thread zachb1996--- via Python-ideas
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-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/4JN76TMBGA3YDN5445OR2S7OT32JH2XO/
Code of Conduct: http://python.org/psf/codeofconduct/