嵌套函数在哪里生效? [英] Where do nested functions live?

查看:54
本文介绍了嵌套函数在哪里生效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个嵌套函数:


def foo():

def bar():

return" bar"

return" foo" + bar()


哪个有效。知道Python如何喜欢命名空间,我想我可以这样做:

这个:


>> foo.bar()



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

AttributeError:''function''对象没有属性''bar''


但它没有像我预期的那样工作。

嵌套函数在哪里生活?你如何访问它们,例如,

读取他们的doc字符串?


-

Steven。

解决方案

Steven D''Aprano写道:


我定义了一个嵌套函数:


def foo():

def bar():

返回" bar"

返回foo + bar()


哪个有效。知道Python如何喜欢命名空间,我想我可以这样做:

这个:


>>> ; foo.bar()



Traceback(最近一次调用最后一次):

文件"< stdin>" ;,第1行,在?

AttributeError:''function''对象没有属性''bar''


但它不起作用正如我所料。


嵌套函数在哪里生活?



执行函数的局部变量,就像变量

" bar"在以下功能中:


def foo():

bar ="我是谁?我住在哪里?"


(是的,每次执行外部

函数时,都会创建一个内部函数。但它是'由预制件制成,所以'不是一个非常昂贵的过程。)


< / F>


" Steven D''Aprano" < st *** @ REMOVE.THIS.cybersource.com.auwrites:


我定义了一个嵌套函数:


def foo():

def bar():

返回" bar"

return" foo" + bar()


哪个有效。知道Python如何喜欢命名空间,我想我可以这样做:$ block $ class =post_quotes>


> foo.bar ()



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

AttributeError:''function''对象没有属性''bar''


但是它没有像我预期的那样工作。



函数不会像

类那样自动添加属性。主要的例外是''__doc__''属性,指的是

doc字符串值。


嵌套函数在哪里生效?



它们位于函数范围内。无法从外面进入,

这是应该的。函数与外界交互

通过一个严格定义的界面,由他们的输入参数定义

及其返回值。


如何访问它们,例如,阅读他们的文档字符串?



如果你想要一些可以调用*和*定义其属性的东西,

你想要比默认函数类型更复杂的东西。定义

a类具有'__call__''属性,制作一个实例,并且

你将能够访问属性并像函数一样调用它。


-

\写一本书就像洗大象一样:没有好处|

` \\ \\开始或结束的地方,并且很难跟踪你已经涵盖的内容。

_o__)。 - 匿名|

Ben Finney


Steven D''Aprano写道:


我定义了一个嵌套函数:


def foo():

def bar():

返回" bar"

return" foo" + bar()


哪个有效。知道Python如何喜欢命名空间,我想我可以这样做

这个:


> ;>>> foo.bar()



回溯(最近一次调用最后一次):

文件" ;< stdin>",第1行,在?

AttributeError:''function''对象没有属性''bar''


但是它没有像我预期的那样工作。


嵌套函数在哪里生活?你如何访问它们,例如,

读取他们的文档字符串?



它没有活着任何地方:如果我写了这个函数


def foo():

locvar = 23

返回locvar


你希望能够访问foo.locvar吗?


这是完全相同的事:声明


def bar():


在调用foo()函数之前不会被执行,并且它的执行

绑定名称栏在foo'的本地命名空间中定义的函数。


问候

Steve


-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://holdenweb.blogspot.com

最近的Ramblings http://del.icio.us/steve.holden


I defined a nested function:

def foo():
def bar():
return "bar"
return "foo " + bar()

which works. Knowing how Python loves namespaces, I thought I could do
this:

>>foo.bar()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: ''function'' object has no attribute ''bar''

but it doesn''t work as I expected.
where do nested functions live? How can you access them, for example, to
read their doc strings?

--
Steven.

解决方案

Steven D''Aprano wrote:

I defined a nested function:

def foo():
def bar():
return "bar"
return "foo " + bar()

which works. Knowing how Python loves namespaces, I thought I could do
this:

>>>foo.bar()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: ''function'' object has no attribute ''bar''

but it doesn''t work as I expected.

where do nested functions live?

in the local variable of an executing function, just like the variable
"bar" in the following function:

def foo():
bar = "who am I? where do I live?"

(yes, an inner function is *created* every time you execute the outer
function. but it''s created from prefabricated parts, so that''s not a
very expensive process).

</F>


"Steven D''Aprano" <st***@REMOVE.THIS.cybersource.com.auwrites:

I defined a nested function:

def foo():
def bar():
return "bar"
return "foo " + bar()

which works. Knowing how Python loves namespaces, I thought I could
do this:

>foo.bar()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: ''function'' object has no attribute ''bar''

but it doesn''t work as I expected.

Functions don''t get attributes automatically added to them the way
class do. The main exception is the ''__doc__'' attribute, referring to
the doc string value.

where do nested functions live?

They live inside the scope of the function. Inaccessible from outside,
which is as it should be. Functions interact with the outside world
through a tightly-defined interface, defined by their input parameters
and their return value.

How can you access them, for example, to read their doc strings?

If you want something that can be called *and* define its attributes,
you want something more complex than the default function type. Define
a class that has a ''__call__'' attribute, make an instance of that, and
you''ll be able to access attributes and call it like a function.

--
\ "Writing a book is like washing an elephant: there no good |
`\ place to begin or end, and it''s hard to keep track of what |
_o__) you''ve already covered." -- Anonymous |
Ben Finney


Steven D''Aprano wrote:

I defined a nested function:

def foo():
def bar():
return "bar"
return "foo " + bar()

which works. Knowing how Python loves namespaces, I thought I could do
this:

>>>>foo.bar()


Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: ''function'' object has no attribute ''bar''

but it doesn''t work as I expected.
where do nested functions live? How can you access them, for example, to
read their doc strings?

It doesn''t "live" anywhere: if I wrote the function

def foo():
locvar = 23
return locvar

would you expect to be able to access foo.locvar?

It''s exactly the same thing: the statement

def bar():

isn''t executed until the foo() function is called, and its execution
binds the name bar in foo''s local namespace to the function that is defined.

regards
Steve

--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden


这篇关于嵌套函数在哪里生效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆