[英]Python: defining a function inside of a function
I am trying to write a function that can define functions. 我正在尝试编写一个可以定义函数的函数。
I tried this: 我尝试了这个:
def function_writer():
print "I am a function writer"
def new_function():
print "I am a new function"
pass
pass
function_writer()
new_function()
However, my debugger complains that new_function is not defined and so this obviously is not the way to do it. 但是,我的调试器抱怨没有定义new_function,因此这显然不是实现它的方法。
Does anyone know how to do this? 有谁知道如何做到这一点?
Thanks. 谢谢。
Your code fails for for exactly the same reason that the following code complains that i
is not defined: 您的代码失败的原因与以下代码抱怨未定义
i
原因完全相同 :
def integer_writer():
i = 0
integer_writer()
i
i
is a local variable in the function integer_writer
, and new_function
is a local variable in the function function_writer
. i
是integer_writer
函数中的局部变量,而new_function
是function_writer function_writer
的局部变量。
You could do the following: 您可以执行以下操作:
def function_writer():
print "I am a function writer"
def new_function():
print "I am a new function"
return new_function
new_function = function_writer()
new_function()
In this case there doesn't seem to be much point (just as there wouldn't normally be much point defining a function that always returns 0
), but if function_writer
were to take some parameters, and if those parameters were used in the function it returns, then you'd be in business. 在这种情况下,似乎没有太多意义(就像定义通常返回
0
的函数通常不会有太多意义一样),但是如果function_writer
要采用一些参数,以及这些参数是否在函数中使用它返回,那么您就可以做生意。
def function_writer():
print "I am a function writer"
def new_function():
print "I am a new function"
return new_function
fn_dynamic = function_writer()
fn_dynamic() #call new_function
maybe what you are looking for? 也许您在寻找什么? (this is almost what decorators do ... except they modify a passed in method)
(这几乎是装饰器的工作……除了它们修改传入的方法外)
another solution (this is not a good suggestion but answers the question) 另一个解决方案(这不是一个好的建议,但可以回答问题)
def function_writer():
print "I am a function writer"
def new_function():
print "I am a new function"
globals()['new_function'] = new_function
function_writer()
new_function()
this type of functionality is typically avoided in python unless you have a VERY COMPELLING reason why this is absolutely what you need ... 在python中通常避免这种类型的功能,除非您有一个非常令人震惊的原因,这绝对是您所需要的...
Of course it will result in an error because new_function
is limited to function_writer
scope. 当然,这将导致错误,因为
new_function
仅限于function_writer
范围。
def function_writer():
print "I am a function writer"
def new_function():
print "I am a new function"
pass
return new_function
a = function_writer()
#output: "I am a function writer"
a()
#output: "I am a new function"
It depends what you plan to do with it. 这取决于您打算如何处理。
You can call it inside the function. 您可以在函数内部调用它。
That may be the case if the inner function needs to access local variables of the outer one. 如果内部函数需要访问外部函数的局部变量,则可能是这种情况。
You can return it in order to be called from outside. 您可以返回它以便从外部调用。
That is quite usual eg in the case of decorators. 这是很常见的,例如在装饰器的情况下。
If you want to create a function factory, just return the created function! 如果要创建函数工厂,只需返回创建的函数!
def factory():
def new_func():
print 'Hello!'
return new_func
f = factory()
f()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.