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


猜你喜欢
- 标准库Python拥有一个强大的标准库。Python语言的核心只包含数字、字符串、列表、字典、文件等常见类型和函数,而由Python标准库提
- 以country.xml为例,内容如下:<?xml version="1.0"?><data>
- 一、安装pyinstallerPyInstaller是一个用来将Python程序打包成一个独立可执行文件的第三方包。因是第三方包,所以需要安
- 不知道大家在做页面的时候会不会遇到样式定义不生效的问题,基本的表现就是怎么改样式都没显示或只有某些浏览器正常,这时通常需要做下面的几步:确认
- PyQt5状态栏控件QStatusBar简介MainWindow对象在底部保留有一个水平条,作为状态栏(QstatusBar),用于显示永久
- 最近有个Vue项目中会偶尔出现Loading chunk {n} failed的报错,报错来自于webpack进行code spilt之后某
- 本文研究的主要是python处理csv数据动态显示曲线,分享了实现代码,具体如下。代码:# -*- coding: utf-8 -*- &q
- 通常绘制二维曲线的时候可以使用matplotlib,不过如果电脑上安装了pandas的话可以直接使用Series的绘图方法进行图像的绘制。p
- --为空的值text ntext select * from lf_newsNg_utf where datalength(newsCont
- 说明1、如果数据集是高维度的,选择谱聚类是子空间的一种。2、如果数据量是中小型的,比如在100W条以内,K均值会是更好的选择;如果数据量超过
- 最近开始实习,工作技术栈主要Python和Golang,目前的任务把Python模块重构为GO模块,然后出现了一个问题,就是要将一个结构体按
- 本文实例讲述了Go语言单链表实现方法。分享给大家供大家参考。具体如下:1. singlechain.go代码如下:////////////单
- 一、概念它们都是Element的属性,表示元素的宽度:Element.clientWidth 内容+内边距-滚动条
- 前言:今天带大家使用JavaScript定制一款网页时钟一、效果展示二、使用的技术主要使用了js的日期对象,实现的时候先创建一个日期对象,并
- 今天我们用python和python的工具包pygame来编写一个贪吃蛇的小游戏贪吃蛇游戏功能介绍贪吃蛇的游戏规则如下:通过上下左右键或者W
- 本文实例讲述了nodejs简单实现TCP服务器端和客户端的聊天功能。分享给大家供大家参考,具体如下:服务器端var net = requir
- 众所周知,Python使用pip方法安装第三方包时,需要从https://pypi.org/资源库中下载,但是会面临下载速度慢,甚至无法下载
- 一、概述现有一个用户表,需要将表数据写入到excel中。环境说明mysql版本:5.7端口:3306数据库:test表名:users表结构如
- 来蓝色一直都在潜水,很少写帖子,太对不起大家了.下面和大家探讨一个话题,希望能引起大家的兴趣.关于H1,一直都想和大家讨论H1用法的问题,可
- /** * The maximum supported {@code LocalTime}, '23:59:59.999999999