如何通过几个函数传递多个参数

时间:2016-02-17 17:32:10

标签: python python-3.x

假设我们有一个暴露的函数(级别0)。我们用各种参数调用这个函数。在内部,此函数调用第二个函数(级别1),但除了使用它们作为参数调用第三个函数(级别2)之外,不使用任何给定参数。然而,它可能会做一些其他的事情。

我的问题是。如何在不在中间层功能(级别1)中创建太多噪声的情况下传递参数?我列出了一些可能的方法。但请注意,其中一些是相当丑陋的,只是出于完整性的原因。我正在寻找一些既定的指导方针而不是个人对该主题的个人意见

# Transport all of them individually down the road.
# This is the most obvious way. However the amount of parameter increases the
# noise in A_1 since they are only passed along
def A_0(msg, term_print):
    A_1(msg, term_print)

def A_1(msg, term_print):
    A_2(msg, term_print)

def A_2(msg, term_print):
    print(msg, end=term_print)


# Create parameter object (in this case dict) and pass it down.
# Reduces the amount of parameters. However when only reading the source of B1
# it is impossible to determine what par is
def B_0(msg, term_print):
    B_1({'msg': msg, 'end': term_print})

def B_1(par):
    B_2(par)

def B_2(par):
    print(par['msg'], end=par['end'])


# Use global variables. We all know the pitfalls of global variables. However
# in python there are at least limited to their module
def C_0(msg, term_print):
    global MSG, TERM_PRINT

    MSG = msg
    TERM_PRINT = term_print
    C_1()

def C_1():
    C_2()

def C_2():
    print(MSG, end=TERM_PRINT)


# Use the fact that python creates function objects. We can now append those
# objects. This makes some more 'localised' variables than shown before. However
# this also makes the code harder to maintain. When we change D_2 we have to alter
# D_0 as well even though it never directly calls it
def D_0(msg, term_print):
    D_2.msg = msg
    D_2.term_print = term_print
    D_1()

def D_1():
    D_2()

def D_2():
    print(D_2.msg, end=D_2.term_print)


# Create a class with the functions E_1, E_2 to enclose the variables.
class E(dict):
    def E_1(self):
        self.E_2()

    def E_2(self):
        print(self['msg'], end=self['end'])

def E_0(msg, term_print):
    E([('msg', msg), ('end', term_print)]).E_1()


# Create a nested scope. This make it very hard to read the function. Furthermore
# F_1 cannot be called directly from outside (without abusing the construct)
def F_0(msg, term_print):
    def F_1():
        F_2()

    def F_2():
        print(msg, end=term_print)

    F_1()


A_0('What', ' ')
B_0('is', ' ')
C_0('the', ' ')
D_0('best', ' ')
E_0('way', '')
F_0('?', '\n')

2 个答案:

答案 0 :(得分:3)

如果不知道为什么有如此多的参数和如此多的功能级别的全部细节,很难给出完整的答案。但一般来说,传递太多参数被视为code smell

通常,如果一组函数都使用相同的参数,则意味着它们以某种方式密切相关,并且可以从封装类中的参数中受益,以便所有相关方法可以共享该数据。

  

TooManyParameters通常是CodeSmell。如果你必须通过这么多   数据在一起,它可以表明数据在某种程度上是相关的   想要封装在自己的类中。传递一个   属于分开的数据结构并不能解决问题。相反,   这个想法是属于一起的东西,保持在一起;事   属于分开,分开;根据OneResponsibilityRule。

实际上,如果他们所做的只是将数据传递给其他函数,你可能会发现完全没有必要使用整个函数。

class A(object):

    def __init__(self, msg, term_print)
        super(A, self).__init__()
        self.msg = msg
        self.term_print = term_print

    def a_0():
        return self.a_1()

    def a_1():
        return self.a_2()

    def a_2():
        print msg, term_print

答案 1 :(得分:0)

根据您的参数集和函数A0的含义,使用*args表示法也可能是一个选项:

def A0(*args):
    A1(*args)

这允许将任意数量的参数传递给A0并将它们传递给A1不变。如果A0的语义就是那个,那么*表示法最能表达意图。但是,如果您要以不同的顺序传递参数或对它们执行任何其他操作,除了将它们作为不透明序列传递之外,这种表示法不太合适。