避免方法中过多的参数

时间:2019-02-11 02:35:32

标签: python class object methods

我想知道是否有一种方法可以避免将过多的参数传递给方法,当前我传递的是5,我认为这样做会使代码更难维护且不太干净。在这种情况下,方法为generic_column。 目前传递了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)

我想找到一种方法,使其他需要处理前面代码的人更容易维护和清除它们。

1 个答案:

答案 0 :(得分:0)

为什么不**kwargs

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