Python—将参数从一个函数传递到嵌套函数调用
我想用Python做一些类似于的事情 我想将一个参数从function1abc传递到function2,作为type=abc 伪代码如下:Python—将参数从一个函数传递到嵌套函数调用,python,Python,我想用Python做一些类似于的事情 我想将一个参数从function1abc传递到function2,作为type=abc 伪代码如下: function1 (*args, abc): print xyz function2(type=abc) 根据您的伪代码: def function2(type): print type def function1(abc, *args): print "something" function2(type=abc) &g
function1 (*args, abc):
print xyz
function2(type=abc)
根据您的伪代码:
def function2(type):
print type
def function1(abc, *args):
print "something"
function2(type=abc)
>>> function1("blah", 1, 2, 3)
something
blah
但根据你的相关问题,也许你想通过varargs:
def function2(type, *args):
print type, args
def function1(abc, *args):
print "something"
function2(abc, *args)
>>> function1("blah", 1, 2, 3)
something
blah (1, 2, 3)
根据您的伪代码:
def function2(type):
print type
def function1(abc, *args):
print "something"
function2(type=abc)
>>> function1("blah", 1, 2, 3)
something
blah
但根据你的相关问题,也许你想通过varargs:
def function2(type, *args):
print type, args
def function1(abc, *args):
print "something"
function2(abc, *args)
>>> function1("blah", 1, 2, 3)
something
blah (1, 2, 3)
Python是动态类型的。不过,类型转换是一种选择
def foo(bar)
foo_bar(str(bar))
Python是动态类型的。不过,类型转换是一种选择
def foo(bar)
foo_bar(str(bar))
除了function1中的参数顺序之外,伪代码还有什么问题?python是一种动态语言,因此不需要传递对象的类型。只需使用func1*args就可以了。如果您想处理类型,请检查func2内部的代码:typeargs[1]==abc1除了function1中的参数顺序之外,伪代码有什么问题?python是一种动态语言,因此您不需要传递对象的类型。只需使用func1*args就可以了。如果要处理该类型,请在func2中按代码进行检查:typeargs[1]==abc