类中的函数实例变量 [英] Function instance variables inside a class

查看:56
本文介绍了类中的函数实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的方法中实现一个所谓的静态变量,类似于 此 Stackoverflow 线程中描述的装饰器方法.具体来说,我定义了一个装饰器函数如下:

def static_var(varName, value):定义装饰(功能):setattr(函数,变量名,值)返回函数返回装饰

如示例所示,这可用于将变量附加到函数:

@static_var('seed', 0)定义计数器():counter.seed +=1返回计数器.seed

这个方法会返回它被调用的次数.

我遇到的问题是,如果我在类中定义方法,这将不起作用:

class Circle(object):@static_var('种子',0)定义计数器(自己):counter.seed +=1返回计数器.seed

如果我实例化一个 Circle 并运行 counter

<预><代码>>>>>myCircle = 圆()>>>>myCircle.counter()

我收到以下错误:NameError: global name 'counter' is not defined.

我对此的回应是,也许我需要使用 self.counter,即

class Circle(object):@static_var('种子',0)定义计数器(自己):self.counter.seed +=1返回 self.counter.seed

然而这会产生错误,AttributeError: 'instancemethod' object has no attribute 'seed'.

这里发生了什么?

解决方案

您想访问函数对象,但您访问的是方法.Python 将实例和类上的函数视为 描述符,在查找时返回绑定方法.

使用:

@static_var('seed',0)定义计数器(自己):self.counter.__func__.seed += 1

到达被包装的函数对象.

在 Python 3 中,您还可以访问类上的函数对象:

@static_var('seed',0)定义计数器(自己):Circle.counter.seed += 1

在 Python 2 中,它仍然会返回一个未绑定的方法对象(一个没有附加实例的方法).

当然,仅仅因为您可以做到这一点,并不一定使它成为一个好主意.有了一个方法,你就有了一个类,给你一个替代的位置来存储那个计数器.你可以把它放在Countertype(self) 上,后者会给你一个每个子类的计数器.

I'm trying to implement a so-called static variable in my method, similar to the decorator method described in this Stackoverflow thread. Specifically, I define a decorator function as follows:

def static_var(varName, value):
    def decorate(function):
        setattr(function,varName,value)
        return function
    return decorate

As the example shows, this can be used to attach a variable to the function:

@static_var('seed', 0)
def counter():
    counter.seed +=1
    return counter.seed

This method will return the number of times it has been called.

The issue i am having is that this does not work if I define the method inside a class:

class Circle(object):

    @static_var('seed',0)
    def counter(self):
        counter.seed +=1
        return counter.seed

If I instantiate a Circle and run counter,

>>>> myCircle = Circle()
>>>> myCircle.counter()

I get the following error: NameError: global name 'counter' is not defined.

My response to this was that maybe I need to use self.counter, i.e.

class Circle(object):

    @static_var('seed',0)
    def counter(self):
        self.counter.seed +=1
        return self.counter.seed

However this produces the error, AttributeError: 'instancemethod' object has no attribute 'seed'.

What is going on here?

解决方案

You want to access the function object, but you are instead accessing a method. Python treats functions on instances and classes as descriptors, returning bound methods at lookup time.

Use:

@static_var('seed',0)
def counter(self):
    self.counter.__func__.seed += 1

to reach the wrapped function object.

In Python 3, you can also access the function object on the class:

@static_var('seed',0)
def counter(self):
    Circle.counter.seed += 1

In Python 2 that'd still return an unbound method object (a method without an instance attached).

Of course, just because you can do this, does not necessarily make it a good idea. With a method you have a class, giving you an alternative location to store that counter. You could put it on either Counter or on type(self), where the latter would give you a counter per subclass.

这篇关于类中的函数实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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