简体   繁体   English

避免方法中过多的参数

[英]Avoid too many arguments in a method

I would like to know if there is a way to avoid to pass too many arguments to a method, currently I am passing 5, which I consider would make the code harder to maintain and less clean. 我想知道是否有一种方法可以避免将过多的参数传递给方法,当前我传递的是5,我认为这样做会使代码更难维护且不太干净。 In this case the method generic_column. 在这种情况下,方法为generic_column。 Currently passes 5 arguments but it might pass 8 or more. 目前传递了5个参数,但可能传递了8个或更多参数。

class BranchBuilder(object):
    """docstring forBranchBuilder."""
    def __init__(self, raw):
        self.raw = raw

    @staticmethod
    def generic_column(size, posx, distance, raw=True,
                       color="red", complex=True):
        """Build the column according the needed requirements."""
    # creates column with arguments.
        return generic_column

    def builder(self):
        """Build the branch that contains all the columns."""
        initial_column = self.generic_column(3, 120, 66, raw=True,
                                             color="white", complex=False)
        mid_column = self.generic_column(3, 120, 66, raw=False, color="black",
                                         complex=False)
        last_column = self.generic_column(3, 120, 66, raw=False, complex=True)

I would like to found a way to make it easier to maintain and clear to any other person who has to deal with the code ahead. 我想找到一种方法,使其他需要处理前面代码的人更容易维护和清除它们。

Why not **kwargs : 为什么不**kwargs

def generic_column(**kwargs):
    """Build the column according the needed requirements."""
# creates column with arguments.
    return generic_column

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM