Hello guys,
First time for me to submit a feature request for python.
A lot of time we try to use a member variable as the default value of a member function argument. So we write the code like this:
```python
class A:
def __init__(self, a: int = 3):
self.a = a
def func(self, a: int = None):
if a is None:
a = self.a
a += 1
return a
```
This seems okay, but when there are more than 10 arguments need to set their default values as correlated member variables (with the same name at most times), it'll be very painful and the codes turn out to be quite ugly.
I wish a clean coding style, so I write something like this
```python
def func(self, a: int = 'self'):
for k, v in process(a=a, b=b):
eval(f'{k} = v')
a += 1
return a
def process(self, **kwargs):
return {k: getattr(self, k) if (v == 'self' and k in self.__dict__.keys()) else v for k, v in kwargs.items()}
```
That seems absolutely much worse for sure!
I dream I can just write
```python
def func(self, a: int = 'self'):
a += 1
return a
```
where I know the default value cannot be 'self', but a special flag which won't get conflict with any other valid argument values. I don't know what it should be though.
Some related questions:
python:
https://stackoverflow.com/questions/39928345/python-member-variable-as-parameter
C++:
https://stackoverflow.com/questions/32399730/default-arguments-as-non-static-member-variables
Let me know your ideas!
Ren Pang
_______________________________________________
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/CTLUXDTMIRRK6AU7OYBA66FHCLG5GZLF/
Code of Conduct: http://python.org/psf/codeofconduct/