python静态方法实例
作者:shichen2014 发布时间:2023-02-17 12:03:22
标签: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程序设计有所帮助。
0
投稿
猜你喜欢
- 换脸!这段时间,deepfakes搞得火热,比方说把《射雕英雄传》里的朱茵换成了杨幂,看下面的图!毫无违和感!其实早在之前,基于AI换脸的技
- 以下的文章主要介绍的是MySQL 查询缓存的实际应用代码以及查看MySQL 查询缓存的大小 ,碎片整理,清除缓存以及监视MySQL 查询缓存
- 一、文章概述本文将要讲述的是Python环境下如何用OpenCV检测人脸,本文的主要内容分为:1、检测图片中的人脸2、实时检测视频中出现的人
- 首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真TM的都是头头是道,什么参
- lambda函数的定义   lambda函数是Python中常用的内置函数,又称为匿名
- jscript 5.7 发布修复了不少ie javascript内存泄露的问题。但是leak依然存在。当我们频繁使用 setInterval
- 在ASP中,为什么有时候刷新页面后,重新执行ASP代码时就连不上数据库了?这种情况一般发生在动态IP分配中,由于ASP连接数据库是定时的,默
- 元数据简介元数据 (metadata) 最常见的定义为“有关数据的结构数据”,或者再简单一点就是“关于数据的信息”,日常生活中的图例、图书馆
- 适配器模式Adapter Pattern是什么适配器模式是一种结构型模式,它可以将一个类的接口转换成客户端所期望的接口,从而使原本不兼容的类
- 什么是设计模式设计模式是针对软件开发中出现的常见问题的可重用解决方案。它们并不特定于任何编程语言或框架,而是描述了可应用于各种情况的一般原则
- 前言文件上传漏洞大多出现在可以进行文件上传的地方,如用户头像上传,文档上传处等。该漏洞是一个危害十分大的漏洞,通过文件上传,攻击者可以上传w
- 超链接在新窗口打开,是在<a>标签加 target="_blank" 即可。可按下“POST/GET提交按钮
- asp十进制转二进制;二进制转十进制;二进制转十六进制;十六进制转二进制;八进制转二进制'二进制转八进制;八进制转十进制;十六进制转
- 设计页面时,经常会从一个页面打开一个子窗口以供浏览者查看。通常,这种子窗口中的内容一经浏览者看过,对于浏览者而言就不再需要,而他们常常会忘记
- 小渣渣复现大佬project发现GPU跑不动,出现如下报错:RuntimeError: CUDA out of memory.看下来最简单粗
- JavaScript图片水平翻转后垂直翻反转的特效一:<!--把下列代码加到body区域内--><SCRIPT langu
- 一、前言在Python提供了re模块,用于实现正则表达式的操作。在实现时,可以使用re模块提供的方法(如,search()、match()、
- 本文实例分析了Python自定义进程池。分享给大家供大家参考,具体如下:代码说明一切:#encoding=utf-8#author: wal
- 作为一个诞生已有 30 余年的编程语言,Python 的流行度在近几年迎来了 * 性的增长。为了方便大家能够更好的采用 Python,利用该语
- php二分查找示例二分查找常用写法有递归和非递归,在寻找中值的时候,可以用插值法代替求中值法。当有序数组中的数据均匀递增时,采用插值方法可以