我需要一个 Python 类来跟踪它被实例化的次数

发布于 2024-07-25 12:45:39 字数 285 浏览 11 评论 0原文

我需要一个像这样工作的类:

>>> a=Foo()
>>> b=Foo()
>>> c=Foo()
>>> c.i
3

这是我的尝试:

class Foo(object):
    i = 0
    def __init__(self):
        Foo.i += 1

它按要求工作,但我想知道是否有更Pythonic的方法来做到这一点。

I need a class that works like this:

>>> a=Foo()
>>> b=Foo()
>>> c=Foo()
>>> c.i
3

Here is my try:

class Foo(object):
    i = 0
    def __init__(self):
        Foo.i += 1

It works as required, but I wonder if there is a more pythonic way to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷爱 2024-08-01 12:45:43

如果您想担心线程安全(以便可以从实例化 Foo 的多个线程修改类变量),那么上面的答案是正确的。 我在此处提出了有关线程安全的问题。 总之,您必须执行以下操作:

from __future__ import with_statement # for python 2.5

import threading

class Foo(object):
  lock = threading.Lock()
  instance_count = 0

  def __init__(self):
    with Foo.lock:
      Foo.instance_count += 1

现在 Foo 可以从多个线程实例化。

If you want to worry about thread safety (so that the class variable can be modified from multiple threads that are instantiating Foos), the above answer is in correct. I asked this question about thread safety here. In summary, you would have to do something like this:

from __future__ import with_statement # for python 2.5

import threading

class Foo(object):
  lock = threading.Lock()
  instance_count = 0

  def __init__(self):
    with Foo.lock:
      Foo.instance_count += 1

Now Foo may be instantiated from multiple threads.

恏ㄋ傷疤忘ㄋ疼 2024-08-01 12:45:43

我们可以使用装饰器吗? 例如..

class ClassCallCount:
    def __init__(self,dec_f):
        self._dec_f = dec_f
        self._count = 0

    def __call__(self, *args, **kwargs):
        self._count +=1
        return self._dec_f(*args, **kwargs)

    def PrintCalled(self):
        return (self._count)


@ClassCallCount
def somefunc(someval):
    print ('Value : {0}'.format(someval))



    somefunc('val.1')
    somefunc('val.2')
    somefunc('val.3')
    somefunc('val.4')
    ## Get the # of times the class was called
    print ('of times class was called : {0}'.format(somefunc._count))

Could we use decorators ? So for example ..

class ClassCallCount:
    def __init__(self,dec_f):
        self._dec_f = dec_f
        self._count = 0

    def __call__(self, *args, **kwargs):
        self._count +=1
        return self._dec_f(*args, **kwargs)

    def PrintCalled(self):
        return (self._count)


@ClassCallCount
def somefunc(someval):
    print ('Value : {0}'.format(someval))



    somefunc('val.1')
    somefunc('val.2')
    somefunc('val.3')
    somefunc('val.4')
    ## Get the # of times the class was called
    print ('of times class was called : {0}'.format(somefunc._count))
来日方长 2024-08-01 12:45:42

滥用装饰器和元类。

def counting(cls):
    class MetaClass(getattr(cls, '__class__', type)):
        __counter = 0
        def __new__(meta, name, bases, attrs):
            old_init = attrs.get('__init__')
            def __init__(*args, **kwargs):
                MetaClass.__counter += 1
                if old_init: return old_init(*args, **kwargs)
            @classmethod
            def get_counter(cls):
                return MetaClass.__counter
            new_attrs = dict(attrs)
            new_attrs.update({'__init__': __init__, 'get_counter': get_counter})
            return super(MetaClass, meta).__new__(meta, name, bases, new_attrs)
    return MetaClass(cls.__name__, cls.__bases__, cls.__dict__)

@counting
class Foo(object):
    pass

class Bar(Foo):
    pass

print Foo.get_counter()    # ==> 0
print Foo().get_counter()  # ==> 1
print Bar.get_counter()    # ==> 1
print Bar().get_counter()  # ==> 2
print Foo.get_counter()    # ==> 2
print Foo().get_counter()  # ==> 3

通过频繁使用双下划线名称,您可以看出它是 Pythonic 的。 (开玩笑,开玩笑……)

Abuse of decorators and metaclasses.

def counting(cls):
    class MetaClass(getattr(cls, '__class__', type)):
        __counter = 0
        def __new__(meta, name, bases, attrs):
            old_init = attrs.get('__init__')
            def __init__(*args, **kwargs):
                MetaClass.__counter += 1
                if old_init: return old_init(*args, **kwargs)
            @classmethod
            def get_counter(cls):
                return MetaClass.__counter
            new_attrs = dict(attrs)
            new_attrs.update({'__init__': __init__, 'get_counter': get_counter})
            return super(MetaClass, meta).__new__(meta, name, bases, new_attrs)
    return MetaClass(cls.__name__, cls.__bases__, cls.__dict__)

@counting
class Foo(object):
    pass

class Bar(Foo):
    pass

print Foo.get_counter()    # ==> 0
print Foo().get_counter()  # ==> 1
print Bar.get_counter()    # ==> 1
print Bar().get_counter()  # ==> 2
print Foo.get_counter()    # ==> 2
print Foo().get_counter()  # ==> 3

You can tell it's Pythonic by the frequent use of double underscored names. (Kidding, kidding...)

森末i 2024-08-01 12:45:41

没有。 那很好。

来自《Python 之禅》:“简单胜于复杂。”

这很好用,而且很清楚你在做什么,不要让它复杂化。 也许将其命名为 counter 或其他名称,但除此之外,您可以尽可能地使用 pythonic。

Nope. That's pretty good.

From The Zen of Python: "Simple is better than complex."

That works fine and is clear on what you're doing, don't complicate it. Maybe name it counter or something, but other than that you're good to go as far as pythonic goes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文