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程序设计有所帮助。

时间: 2024-08-02 03:07:29

python静态方法实例_python的相关文章

线程和进程的区别及Python代码实例_python

在程序猿的世界中,线程和进程是一个很重要的概念,很多人经常弄不清线程和进程到底是什么,有什么区别,本文试图来解释一下线程和进程.首先来看一下概念: 进程(英语:process),是计算机中已运行程序的实体.进程为曾经是分时系统的基本运作单位.在面向进程设计的系统(如早期的UNIX,Linux 2.4及更早的版本)中,进程是程序的基本执行实体:在面向线程设计的系统(如当代多数操作系统.Linux 2.6及更新的版本)中,进程本身不是基本运行单位,而是线程的容器.程序本身只是指令.数据及其组织形式的

python多重继承实例_python

本文实例讲述了python多重继承用法,分享给大家供大家参考.具体实现方法如下: 1.mro.py文件如下: #!/usr/bin/python # Filename:mro.py class P1: def foo(self): print 'called P1-foo' class P2: def foo(self): print 'called P2-foo' def bar(self): print 'called P2-bar' class C1(P1, P2): pass class

爬山算法简介和Python实现实例_python

一.爬山法简介 爬山法(climbing method)是一种优化算法,其一般从一个随机的解开始,然后逐步找到一个最优解(局部最优). 假定所求问题有多个参数,我们在通过爬山法逐步获得最优解的过程中可以依次分别将某个参数的值增加或者减少一个单位.例如某个问题的解需要使用3个整数类型的参数x1.x2.x3,开始时将这三个参数设值为(2,2,-2),将x1增加/减少1,得到两个解(1,2,-2), (3, 2,-2):将x2增加/减少1,得到两个解(2,3, -2),(2,1, -2):将x3增加/

python修改注册表终止360进程实例_python

本文实例讲述了python修改注册表终止360进程的实现方法.分享给大家供大家参考. 具体实现代码如下: import _winreg import os import shutil #复制自身 shutil.copyfile(K3.exe,c:WINDOWSsystem32K3.exe) #把360启动改为自身 run = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, "SOFTWAREMicrosoftWindowsCurrentVersionRu

python计算书页码的统计数字问题实例_python

本文实例讲述了python计算书页码的统计数字问题,是Python程序设计中一个比较典型的应用实例.分享给大家供大家参考.具体如下: 问题描述:对给定页码n,计算出全部页码中分别用到多少次数字0,1,2,3,4...,9 实例代码如下: def count_num1(page_num): num_zero = 0 num_one = 0 num_two = 0 num_three = 0 num_four = 0 num_five = 0 num_six = 0 num_seven = 0 nu

python实现的重启关机程序实例_python

本文实例讲述了Python实现的重启关机程序的方法,对Python程序设计有一定的参考价值.具体方法如下: 实例代码如下: #!/usr/bin/python #coding=utf-8 import time from os import system runing = True while runing: input = raw_input('关机(s)OR重启(r)?(q退出)') input = input.lower() if input == 'q' or input =='quit

python实现根据图标提取分类应用程序实例_python

本文实例讲述了python实现根据图标提取分类应用程序,分享给大家供大家参考. 具体方法如下: #!/usr/bin/python # -*- coding: utf-8 -*- import Image import win32ui import win32gui def make_regalur_image(img, size = (256, 256)): return img.resize(size).convert('RGB') def split_image(img, part_siz

python实现通过shelve修改对象实例_python

本文实例讲述了python实现通过shelve修改对象的方法,分享给大家供大家参考. 具体实现方法如下: import shelve she = shelve.open('try.she','c') for c in 'spam': she[c] = {c:23} for c in she.keys(): print c,she[c] she.close() she = shelve.open('try.she','c') print she['p'] she['p']['p'] = 42 #这

Python 制作糗事百科爬虫实例_python

早上起来闲来无事做,莫名其妙的就弹出了糗事百科的段子,转念一想既然你送上门来,那我就写个爬虫到你网站上爬一爬吧,一来当做练练手,二来也算找点乐子. 其实这两天也正在接触数据库的内容,可以将爬取下来的数据保存在数据库中,以待以后的利用.好了,废话不多说了,先来看看程序爬取的数据结果 值得一提的是,我在程序中想一下子爬取糗事百科 30 页的内容,但是出现了连接错误,当我把页数降到 20 页的时候,程序就可以正常的跑起来了,不知道是什么原因,渴望知道的大神可以告诉我一声,感激不尽. 程序非常简单,直接