python实例化次数怎么算,我需要一个Python类来跟踪它被实例化的次数
生活随笔
收集整理的這篇文章主要介紹了
python实例化次数怎么算,我需要一个Python类来跟踪它被实例化的次数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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.
解決方案
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.
總結
以上是生活随笔為你收集整理的python实例化次数怎么算,我需要一个Python类来跟踪它被实例化的次数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言怎么在编码时改变颜色,怎么给贪吃蛇
- 下一篇: c语言两个数组按位合并,合并两个线性表(