On Wed, Dec 16, 2020 at 09:11:18AM -0000, Local-State  wrote:

> 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.

If the code is ugly, that is a hint that having a method with more than 
ten arguments is ugly.

But it isn't *very* ugly; each test only requires one line, or two if 
you spread it out, and very simple code:

    def method(self, spam, eggs, cheese, tomato, aardvark):
        if spam is None: spam = self.spam
        if eggs is None: eggs = self.eggs
        # etc

A bit tedious to write, but it's clear and obvious.


> 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!

Indeed. There is nothing "clean" about that.

Are you sure it works? `eval(f'{k} = v')` will give a SyntaxError:

    >>> eval('name = 1')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1
        name = 1
             ^
    SyntaxError: invalid syntax



-- 
Steve
_______________________________________________
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/3EU6M2IFEC5KVDMU6MOSSC5XQUWO5HTK/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to