不可见的功能属性 [英] Invisible function attributes

查看:82
本文介绍了不可见的功能属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 2.3

def foo():
.... foo.a = 1

.... vars(foo)
{} foo()
vars(foo)
{''a'':1}




因此,在第一次调用函数之前,函数属性看起来并不是真正的
。如果这是

预期的行为,那真的很奇怪。我在LRM中找不到任何关于这个主题的明确讨论。


感谢所有人都能对此有所了解,


- OL

解决方案

Olivier Lefevre写道:

Python 2.3

def foo():
... foo.a = 1
...
vars( foo)
{}
foo()
vars(foo)



{'''':1}

因此,在第一次调用函数之前,函数属性似乎并非真正存在。如果那是
预期的行为,那真的很奇怪。我无法在LRM中找到任何关于这个主题的明确讨论。

如果有人能够对此有所了解,那么,

- OL




对我有意义。


在执行foo()之前,永远不应执行foo.a = 1行。

因此,foo.a永远不会在调用foo之前设置。


- TL


def foo():

尝试:
每次调用函数时执行
foo.a + = 1#

除了AttributeError:

foo.a = 1#如果它还没有那么设置为1


foo.b = 1#执行一次


print vars(foo)#function body尚未调用{''b'':1}


for i in range(3):

foo()

print vars(foo)


循环输出是


{''a'':1,''b'':1}

{''a'':2,''b'':1}

{'''':3,''b'':1这是完美的,因为函数体中的代码永远不会被执行

除非你调用函数,而模块级的代码被执行<在导入模块时立即获得
。所以把foo.attr = ...放到

函数体中,如果你希望它在每次调用函数时都执行

;否则将其放入模块启动代码中,i。即不要缩进

吧。


彼得




Olivier Lefevre <乐****** @ yahoo.com>在消息中写道

news:51 ************************** @ posting.google.c om ...

Python 2.3

def foo():... foo.a = 1



如果你想在调用foo之前将其归结,请将setter

移到函数外部。

def foo():pass
.... foo.a = 1
vars(foo)



{''a'':1}


Terry J Reedy


Python 2.3

def foo(): .... foo.a = 1
.... vars(foo) {} foo()
vars(foo) {''a'': 1}



So it would appear that function attributes are not really
there until the first call to the function. If that is the
intended behaviour, it is really weird. I couldn''t find any
explicit discussion of this topic in the LRM.

Thanks if anyone can shed some light on this,

-- O.L.

解决方案

Olivier Lefevre wrote:

Python 2.3

def foo():
... foo.a = 1
...
vars(foo)
{}
foo()
vars(foo)



{''a'': 1}
So it would appear that function attributes are not really
there until the first call to the function. If that is the
intended behaviour, it is really weird. I couldn''t find any
explicit discussion of this topic in the LRM.

Thanks if anyone can shed some light on this,

-- O.L.



Makes sense to me.

The foo.a = 1 line should never be executed until foo() is executed.
Thus, foo.a is never set before the call to foo.

- TL


def foo():
try:
foo.a += 1 # executed every time you call the function
except AttributeError:
foo.a = 1 # set to one if it''s not already there

foo.b = 1 # executed once

print vars(foo) # function body not yet called {''b'': 1}

for i in range(3):
foo()
print vars(foo)

Loop output is

{''a'': 1, ''b'': 1}
{''a'': 2, ''b'': 1}
{''a'': 3, ''b'': 1}

That is all perfectly sane as code in the function body is never executed
unless you call the function, whereas code on the module level is executed
immediately as the module is imported. So put foo.attr = ... into the
function body iff you want it to execute every time the function is
invoked; otherwise put it into the module startup code, i. e. do not indent
it.

Peter



"Olivier Lefevre" <le******@yahoo.com> wrote in message
news:51**************************@posting.google.c om...

Python 2.3

def foo(): ... foo.a = 1



If you want foo to be attributed before it is called, move the setter
outside the function.

def foo(): pass .... foo.a = 1
vars(foo)


{''a'': 1}

Terry J Reedy


这篇关于不可见的功能属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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