外贸公司网站源码,最新科技新闻消息,邢台规划局网站建设,网站建设后期收什么费用1 python静态方法和类方法
python类方法分为实例方法、类方法、静态方法。
#xff08;1#xff09; 实例方法#xff0c;不用修饰#xff0c;第1个参数为实例对象#xff0c;默认为self。
通过实例调用时#xff0c;自动将当前实例传给self#xff1b;
通过类调用时…1 python静态方法和类方法
python类方法分为实例方法、类方法、静态方法。
1 实例方法不用修饰第1个参数为实例对象默认为self。
通过实例调用时自动将当前实例传给self
通过类调用时需要显式将实例传给self。
2 类方法用classmethod修饰第1个参数为类对象默认为cls。
也可以通过内置函数classmethod(cmeth)将cmeth转为类方法。
通过实例调用时自动将当前类传递给第1个参数
通过类调用时自动将当前类传递给第1个参数。
3 静态方法用staticmethod修饰第1个参数不需要默认无self和cls。
也可以通过内置函数staticmethod(smeth)将smeth转为静态方法。
通过实例调用时不会自动将当前实例传给第1个参数。
通过类调用时不需要传送实例给第1个参数。
python2.2版本新增类方法和静态方法对经典类有效对新式类无效。
1.1 python类方法
python类方法通过classmethod修饰或通过内置函数classmethod()转换。类方法第1个参数为类对象默认为cls。通过实例调用时自动将当前类传递给第1个参数通过类调用时自动将当前类传递给第1个参数。
类方法适合处理每个类中不同的数据通过第1个入参cls完成。
1.2 python静态方法
python静态方法通过staticmethod修饰或通过内置函数staticmehod()转换。静态方法入参无self和cls。通过实例调用时不会自动将当前实例传给第1个参数通过类调用时不需要显式传递实例给第1个参数。
python静态方法用于处理与类而不是与实例相关的数据。
比如记录类创建的实例数。
把计数器作为类属性每次创建实例对象时构造函数对计数器加1.
类属性是所有实例共享的可以被所有实例使用。
1.2.1 类内未使用静态方法的无参方法
描述
python2.x和3.x类的方法未定义第1个入参通过类和实例调用结果不同。
class NoStaticMed:def printNumOfIns():passNO调用方式调用举例python2.xpython3.x1类调用NoStaticMed.printNumOfIns()报错成功2实例调用NoStaticMed ().printNumOfIns()报错报错
示例
staticmedcls.py
# coding:utf-8
import sys
print(python版本为:python{}.format(sys.version.split( )[0]))
class NoStaticMed:numOfInstances0def __init__(self):NoStaticMed.numOfInstances1def printNumOfIns():print(创建的实例数为{}.format(NoStaticMed.numOfInstances))python2.x在idle执行结果 import osos.chdir(rE:\documents\F盘)from staticmedcls import NoStaticMed
python版本为:python2.7.18sm1NoStaticMed()sm2NoStaticMed()sm3NoStaticMed()NoStaticMed.printNumOfIns()
# python 2.x 通过类调用无入参类方法报 无绑定方法 必须传实例作为第1个入参。
Traceback (most recent call last):File pyshell#6, line 1, in moduleNoStaticMed.printNumOfIns()
TypeError: unbound method printNumOfIns() must be called with NoStaticMed instance as first argument (got nothing instead)sm1.printNumOfIns()
# python 2.x 通过实例调用无入参类方法报 收到1个入参。即会自动传入一个实例。
Traceback (most recent call last):File pyshell#7, line 1, in modulesm1.printNumOfIns()
TypeError: printNumOfIns() takes no arguments (1 given)python3.x在idle执行结果 import osos.chdir(rE:\documents\F盘)from staticmedcls import NoStaticMed
python版本为:python3.7.8sm1NoStaticMed()sm2NoStaticMed()sm3NoStaticMed()
# python 3.x 通过类调用无入参类方法成功。NoStaticMed.printNumOfIns()
创建的实例数为3sm1.printNumOfIns()
# python 3.x 通过实例调用无入参类方法报 收到1个入参。即会自动传入一个实例。
Traceback (most recent call last):File pyshell#7, line 1, in modulesm1.printNumOfIns()
TypeError: printNumOfIns() takes 0 positional arguments but 1 was given1.2.2 类外无参方法
描述
在类外定义一个函数用于统计类创建的实例数量。
示例
# coding:utf-8
import sys
print(python版本为:python{}.format(sys.version.split( )[0]))
class OutClassMed:numOfInstances0def __init__(self):OutClassMed.numOfInstances1
def printNumOfIns():print(从 OutClassMed 创建的实例数为{}.format(OutClassMed.numOfInstances))
python2.x在idle执行结果 import os;os.chdir(rE:\documents\F盘)from staticmedcls import OutClassMed,printNumOfIns
python版本为:python2.7.18ocm1,ocm2,ocm3OutClassMed(),OutClassMed(),OutClassMed()printNumOfIns()
从 OutClassMed 创建的实例数为3python3.x在idle执行结果 import os;os.chdir(rE:\documents\F盘)from staticmedcls import OutClassMed,printNumOfIns
python版本为:python3.7.8ocm1OutClassMed();ocm2OutClassMed();ocm3OutClassMed()printNumOfIns()
从 OutClassMed 创建的实例数为31.2.3 内置函数staticmethod和classmethod
描述
使用内置函数staticmethod()转为静态方法
使用内置函数classmethod()转为类方法。
示例
Python2.x在idle执行结果 import sysprint(python版本为:python{}.format(sys.version.split( )[0]))
python版本为:python2.7.15class BuiltInSCMed:def instanceMed(self,x):print(self,x)def staticMed(x):print(x)def clsMed(cls,x):print(cls,x)# 通过内置函数 staticmethod 将 staticMed 转为静态方法staticMedstaticmethod(staticMed)# 通过内置函数 classmethod 将 clsMed 转为类方法staticMedclassmethod(clsMed)biscm1BuiltInSCMed()
# 通过实例调用实例方法biscm1.instanceMed(1)
(__main__.BuiltInSCMed instance at 0x03B71620, 1)
# 通过类调用实例方法BuiltInSCMed.instanceMed(biscm1,2)
(__main__.BuiltInSCMed instance at 0x03B71620, 2)
# 通过类调用静态方法BuiltInSCMed.staticMed(3)
3
# 通过实例调用静态方法biscm1.staticMed(梯阅线条)
梯阅线条
# 通过类调用类方法BuiltInSCMed.clsMed(tyxt.work)
(class __main__.BuiltInSCMed at 0x03CD6650, tyxt.work)
# 通过实例调用类方法biscm1.clsMed(tyxt.work)
(class __main__.BuiltInSCMed at 0x03CD6650, tyxt.work)Python3.x在idle执行结果 import sysprint(python版本为:python{}.format(sys.version.split( )[0]))
python版本为:python3.9.0class BuiltInSCMed:def instanceMed(self,x):print(self,x)def staticMed(x):print(x)def clsMed(cls,x):print(cls,x)staticMedstaticmethod(staticMed)clsMedclassmethod(clsMed)biscm1BuiltInSCMed()biscm1.instanceMed(1)
__main__.BuiltInSCMed object at 0x000001B16B6FEBB0 1BuiltInSCMed.instanceMed(biscm1,2)
__main__.BuiltInSCMed object at 0x000001B16B6FEBB0 2BuiltInSCMed.staticMed(3)
3biscm1.staticMed(梯阅线条)
梯阅线条BuiltInSCMed.clsMed(tyxt.work)
class __main__.BuiltInSCMed tyxt.workbiscm1.clsMed(tyxt.work)
class __main__.BuiltInSCMed tyxt.work1.2.4 内置函数staticmethod转换的静态方法统计实例
python2.x 和3.x 在idle 执行结果 相同 class CountInsBISM:numOfInstances0def __init__(self):CountInsBISM.numOfInstances1def printNumOfIns():print(创建的实例数为{}.format(CountInsBISM.numOfInstances))printNumOfInsstaticmethod(printNumOfIns)cibs1,cibs2,cibs3CountInsBISM(),CountInsBISM(),CountInsBISM()CountInsBISM.printNumOfIns() # 通过类调用
创建的实例数为3cibs1.printNumOfIns() # 通过实例调用
创建的实例数为31.2.5 内置函数classmethod转换的类方法统计实例
python2.x 和3.x 在idle 执行结果 相同 class CountInsBICM:numOfInstances0def __init__(self):CountInsBICM.numOfInstances1def printNumOfIns(cls):print(创建的实例数为{}.format(cls.numOfInstances))printNumOfInsclassmethod(printNumOfIns) cibc1,cibc2,cibc3CountInsBICM(),CountInsBICM(),CountInsBICM()CountInsBICM.printNumOfIns() # 通过类调用
创建的实例数为3cibc1.printNumOfIns() # 通过实例调用
创建的实例数为31.2.6 统计每个类的实例
通过类方法统计继承中每个类的实例。
需要在继承中每个类各自维护一个实例数属性用于存放各自数据。
示例 class CountInsEC:numOfInstances0def countcls(cls):cls.numOfInstances1def __init__(self):self.countcls()countclsclassmethod(countcls) class SubA(CountInsEC):numOfInstances0def __init__(self):CountInsEC.__init__(self) class SubB(CountInsEC):numOfInstances0 ciec1,ciec2,ciec3CountInsEC(),CountInsEC(),CountInsEC()suba1,suba2SubA(),SubA()subb1SubB()ciec1.numOfInstances,suba1.numOfInstances,subb1.numOfInstances
(3, 2, 1)CountInsEC.numOfInstances,SubA.numOfInstances,SubB.numOfInstances
(3, 2, 1)