函数如何访问自己的属性? [英] How can a function access its own attributes?

查看:28
本文介绍了函数如何访问自己的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从函数作用域内访问python函数对象的属性?

例如让我们

def f():返回一些东西f._x = "foo"f() # ->富"

现在,如果我们想要返回 _x 属性内容foo",必须是什么?如果有可能的话(简单地)

谢谢

更新:

我还想要以下作品:

g = f德尔夫g() # ->富"

更新 2:

声明这是不可能的(如果是这样的话),以及为什么,比提供一种如何伪造它的方法更令人满意,例如使用与函数不同的对象

解决方案

解决方案

使函数的默认参数之一成为对函数本身的引用.

def f(self):返回 self.xf.func_defaults = (f,)

示例用法:

<预><代码>>>>f.x = 17>>>b = f>>>德尔夫>>>乙()17

说明

原发帖者想要一个不需要全局名称查找的解决方案.简单的解决方案

def f():返回 f.x

在每次调用时查找全局变量f,不符合要求.如果 f 被删除,则函数失败.更复杂的 inspect 提议以同样的方式失败.

我们想要的是执行早期绑定并将绑定的引用存储在对象本身中.以下是我们在概念上所做的:

def f(self=f):返回 self.x

上面的self是一个局部变量,所以没有进行全局查找.但是,我们不能按原样编写代码,因为当我们尝试将 self 的默认值绑定到它时,f 尚未定义.相反,我们在定义 f 之后设置默认值.

装饰器

这是一个简单的装饰器来为你做这件事.请注意,self 参数必须排在最后,与方法不同,self 排在最前面.这也意味着如果您的任何其他参数采用默认值,您必须提供默认值.

def self_reference(f):f.func_defaults = f.func_defaults[:-1] + (f,)返回 f@self_referencedef foo(动词, 副词='swiftly', self=None):return '%s %s %s' % (self.subject, 动词, 副词)

示例:

<预><代码>>>>foo.subject = '弗雷德'>>>酒吧 = foo>>>德尔福>>>酒吧('运行')'弗雷德跑得很快'

is it possible to access the python function object attributes from within the function scope?

e.g. let's have

def f():
    return SOMETHING

f._x = "foo"
f()           # -> "foo"

now, what SOMETHING has to be, if we want to have the _x attribute content "foo" returned? if it's even possible (simply)

thanks

UPDATE:

i'd like the following work also:

g = f
del f
g()          # -> "foo"

UPDATE 2:

Statement that it is not possible (if it is the case), and why, is more satisfying than providing a way how to fake it e.g. with a different object than a function

解决方案

Solution

Make one of the function's default arguments be a reference to the function itself.

def f(self):
    return self.x
f.func_defaults = (f,)

Example usage:

>>> f.x = 17
>>> b = f
>>> del f
>>> b()
17

Explanation

The original poster wanted a solution that does not require a global name lookup. The simple solution

def f():
    return f.x

performs a lookup of the global variable f on each call, which does not meet the requirements. If f is deleted, then the function fails. The more complicated inspect proposal fails in the same way.

What we want is to perform early binding and store the bound reference within the object itself. The following is conceptually what we are doing:

def f(self=f):
    return self.x

In the above, self is a local variable, so no global lookup is performed. However, we can't write the code as-is, because f is not yet defined when we try to bind the default value of self to it. Instead, we set the default value after f is defined.

Decorator

Here's a simple decorator to do this for you. Note that the self argument must come last, unlike methods, where self comes first. This also means that you must give a default value if any of your other arguments take a default value.

def self_reference(f):
    f.func_defaults = f.func_defaults[:-1] + (f,)
    return f

@self_reference
def foo(verb, adverb='swiftly', self=None):
    return '%s %s %s' % (self.subject, verb, adverb)

Example:

>>> foo.subject = 'Fred'
>>> bar = foo
>>> del foo
>>> bar('runs')
'Fred runs swiftly'

这篇关于函数如何访问自己的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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