gpt4 book ai didi

python - 如何通过多个函数向下传递多个参数

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:34 25 4
gpt4 key购买 nike

假设我们有一个暴露的函数(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')

最佳答案

如果不了解为什么有这么多参数和这么多层次的函数的完整细节,就很难给出完整的答案。但一般来说,传递太多参数被认为是 code smell .

一般来说,如果一组函数都使用相同的参数,这意味着它们在某种程度上密切相关,并且可能受益于将参数封装在一个类中,以便所有关联的方法可以共享该数据。

TooManyParameters is often a CodeSmell. If you have to pass that muchdata together, it could indicate the data is related in some way andwants to be encapsulated in its own class. Passing in a singledata structure that belongs apart doesn't solve the problem. Rather,the idea is that things that belong together, keep together; thingsthat belong apart, keep apart; per the OneResponsibilityRule.

事实上,如果整个函数只是将数据传递给其他函数,您可能会发现它们完全没有必要。

class A():

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

def a_0(self):
return self.a_1()

def a_1(self):
return self.a_2()

def a_2(self):
print(msg, self.term_print)

关于python - 如何通过多个函数向下传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35463711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com