将超过255个参数传递给函数 [英] Pass more than 255 arguments to a function
问题描述
我只是将参数从def
语句传递给def
语句.我不知道您可以传递的参数数量没有限制.我假设参数是变量.我需要能够传递超过255个参数.有人可以用通俗易懂的方式帮助我,如何解决超过255个参数".谢谢!
I'm simply passing my arguments from def
statement to def
statement. I did not know there was a limit for the amount of arguments that you can pass. I'm assuming arguments are variables. I need to be able to pass more than 255. Can someone help me in layman terms how to work around "more than 255 arguments". Thanks!
代码:
def a():
"Things happening"
variable1
b(variable1)
def b(variable1):
"Things happening"
variable2
c(variable1, variable2)
def c(variable1, variable2):
"Things happening"
variable3
d(variable1, variable2, variable3)
.
.
.
def z5(variable1,...variable256):
print result
main()
推荐答案
Python故意阻止您传递超过255个参数,因为这样做的代码非常笨拙且难以维护.如果一个函数需要那么多参数(或任何接近该数字的参数),则意味着设计中存在缺陷.
Python purposefully prevents you from passing more than 255 arguments because code that does so is very unweildy and hard to maintain. If a function needs that many arguments (or anything close to that number), it means there is a flaw in the design.
如果您确实需要+255个不同的值,则应将它们全部放在列表中,并让您的函数接受该值:
If you truly need +255 different values, then you should put them all in a list and make your function accept that instead:
def z5(list_of_values):
...
z5([val1, val2, val3, ...])
这篇关于将超过255个参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!