Home  >  Article  >  Backend Development  >  python实现类的静态变量用法实例

python实现类的静态变量用法实例

WBOY
WBOYOriginal
2016-06-10 15:12:581287browse

本文实例讲述了python类的静态变量用法。分享给大家供大家参考。具体分析如下:

这里使用静态变量目的是在类中实现一个静态的队列,这里用数组实现,任何时候插入到队列中的数据不会和类的实例有直接关系。

__author__ = 'Administrator'
class CaptchaImage:
  def queue(self,arr=list()):
    return arr
  def InsertCode(self,code):
    self.queue().append(code)
if __name__=='__main__':
  c = CaptchaImage()
  c.InsertCode(1)
  b=CaptchaImage()
  b.InsertCode(2)
  print(b.queue())
  print(c.queue())

代码执行输出结果为:

[1, 2]
[1, 2]

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn