python静态方法实例

 更新时间:2015年01月14日 14:46:32   投稿:shichen2014  
这篇文章主要介绍了python静态方法,实例分析了python静态方法的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了python静态方法。分享给大家供大家参考。

具体实现方法如下:

复制代码 代码如下:
staticmethod Found at: __builtin__
staticmethod(function) -> method
    
    Convert a function to be a static method.
    
    A static method does not receive an implicit first argument.
    To declare a static method, use this idiom:
    
    class C:
    def f(arg1, arg2, ...): ...
    f = staticmethod(f)
    
    It can be called either on the class (e.g. C.f()) or on an
     instance
    (e.g. C().f()).  The instance is ignored except for its class.
    
    Static methods in Python are similar to those found in
     Java or C++.
    For a more advanced concept, see the classmethod builtin.
  
class Employee:
   """Employee class with static method isCrowded"""
 
   numberOfEmployees = 0  # number of Employees created
   maxEmployees = 10  # maximum number of comfortable employees
 
   def isCrowded():
      """Static method returns true if the employees are crowded"""
 
      return Employee.numberOfEmployees > Employee.maxEmployees
 
   # create static method
   isCrowded = staticmethod(isCrowded)
 
   def __init__(self, firstName, lastName):
      """Employee constructor, takes first name and last name"""
 
      self.first = firstName
      self.last = lastName
      Employee.numberOfEmployees += 1
 
   def __del__(self):
      """Employee destructor"""
 
      Employee.numberOfEmployees -= 1    
 
   def __str__(self):
      """String representation of Employee"""
 
      return "%s %s" % (self.first, self.last)
 
# main program
def main():
   answers = [ "No", "Yes" ]  # responses to isCrowded
   
   employeeList = []  # list of objects of class Employee
 
   # call static method using class
   print "Employees are crowded?",
   print answers[ Employee.isCrowded() ]
 
   print "\nCreating 11 objects of class Employee..."
 
   # create 11 objects of class Employee
   for i in range(11):
      employeeList.append(Employee("John", "Doe" + str(i)))
 
      # call static method using object
      print "Employees are crowded?",
      print answers[ employeeList[ i ].isCrowded() ]
 
   print "\nRemoving one employee..."
   del employeeList[ 0 ]
 
   print "Employees are crowded?", answers[ Employee.isCrowded() ]
 
if __name__ == "__main__":
   main()

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

相关文章

  • python sys模块使用方法介绍

    python sys模块使用方法介绍

    sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧
    2023-01-01
  • django序列化时使用外键的真实值操作

    django序列化时使用外键的真实值操作

    这篇文章主要介绍了django序列化时使用外键的真实值操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • python微信跳一跳系列之色块轮廓定位棋盘

    python微信跳一跳系列之色块轮廓定位棋盘

    这篇文章主要为大家详细介绍了python微信跳一跳系列,色块轮廓定位棋盘,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • 深入探讨opencv图像矫正算法实战

    深入探讨opencv图像矫正算法实战

    在机器视觉中,对于图像的处理有时候因为放置的原因导致ROI区域倾斜,这个时候我们会想办法把它纠正为正确的角度视角来,本文主要介绍了opencv图像矫正算法,感兴趣的可以了解一下
    2021-05-05
  • Python pandas中apply函数简介以及用法详解

    Python pandas中apply函数简介以及用法详解

    apply()函数是pandas里面所有函数中自由度最高的函数, apply()函数的参数是一个函数指针,这里可以使用lambda表达式帮助简化代码,下面这篇文章主要给大家介绍了关于Python pandas中apply函数简介以及用法的相关资料,需要的朋友可以参考下
    2022-09-09
  • Python中关键字global和nonlocal的区别详解

    Python中关键字global和nonlocal的区别详解

    这篇文章主要给大家介绍了关于Python中关键字global和nonlocal的区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-09-09
  • python的pstuil模块使用方法总结

    python的pstuil模块使用方法总结

    这篇文章主要介绍了python的pstuil模块使用方法总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 手把手教你使用Python创建微信机器人

    手把手教你使用Python创建微信机器人

    微信,一个日活10亿的超级app,不仅在国内社交独领风骚,在国外社交也同样占有一席之地,今天我们要将便是如何用Python来生成一个微信机器人,感兴趣的朋友跟随小编一起看看吧
    2019-04-04
  • Python的Twisted框架上手前所必须了解的异步编程思想

    Python的Twisted框架上手前所必须了解的异步编程思想

    Twisted是Python世界中人气最高的framework之一,异步的工作模式使其名扬天下,这里为大家总结了Python的Twisted框架上手前所必须了解的异步编程思想,需要的朋友可以参考下
    2016-05-05
  • 互斥锁解决 Python 中多线程共享全局变量的问题(推荐)

    互斥锁解决 Python 中多线程共享全局变量的问题(推荐)

    这篇文章主要介绍了互斥锁解决 Python 中多线程共享全局变量的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09

最新评论