Python 静态、类和数属性方法
一、Python 静态、类和数属性方法
一、静态方法
每次实例化都会开辟一块新的内存空间(有开销),如果当你遇到需要实例化上万个\十万个\百万个时。那么在进行实例化时,就会耗费大量的的内存,这时需要考虑是否真的需要生成这么多实例?此时就需要使用静态方法
- 普通方法
- 可以在实例化后直接调用,并且在方法里可以通过self调用实例变量或类变量
- 静态方法
- 不可以访问实例变量或类变量。"一个不能访问实例变量和类变量的方法,相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法"
- @staticmethod装饰器即可把其装饰的方法变成一个静态方法
- 静态方法即不能访问公有属性,也不能访问实例
- @普通方法 and 静态方法
class Person(object):
def __init__(self,name):
self.name = name
"""定义普通方法."""
# 至少有一个self参数
def ordinary(self):
print("I am %s "%self.name)
"""定义静态方法"""
# 静态方法即不能访问公有属性. 也不能访问实例. 且无默认参数.可写可不写
@staticmethod
def eat(name,foot):
print("%s is eating....%s"%(name,foot))
# 普通方法调用
P = Person("Harry")
P.ordinary()
# 静态方法调用方式1. 无需实例化.直接传参调用
Person.eat("ZhanSan","rice")
输出
C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe E:/Python_Engineering_Review/Day_08/Test.py
I am Harry
ZhanSan is eating....rice
Process finished with exit code 0
相同点:对于所有的方法而言,均属于类(非对象)中。所以在内存中也只保存一份
不同点:方法调用者不同、调用方法时自动传入的参数不同
二、类方法
类方法:只能访问类的公有属性,不能访问实例属性。(不知道实例的存在,无法调用实例的任何属性)
- @Python 静态\类方法
class Person(object):
name = "Yulan"
def __init__(self,name):
self.name = name
"""定义静态方法"""
@staticmethod
# 静态方法即不能访问公有属性. 也不能访问实例
def eat(name,foot):
print("%s is eating....%s"%(name,foot))
"""类方法"""
# 类方法 只能访问类的公有属性. 不能访问实例属性
@classmethod
def work(self):
print("%s is working...."%self.name)
# 静态方法调用方式1. 无需实例化.直接传参调用
Person.eat("Gouer","rice")
# 类方法
P = Person("Harry")
P.work()
三、属性方法
属性方法:用@property把一个方法变成一个静态属性(变量)
- @property属性方法
class Person(object):
def __init__(self,name):
self.name = name
"""属性方法"""
# 属性方法 的作用是把一个方法变成一个静态属性(变量)
@property
def talk(self):
print("%s says"%self.name)
@talk.setter
def talk(self,msg):
print("Set Msg:",msg)
@talk.deleter
def talk(self):
print("delete talk....")
# 执行时调用第一个
P = Person("Harry")
P.talk
# 执行时调用第二个
P.talk = "Hello"
# 删除talk
del P.talk
把一个方法变成静态属性有什么作用呢?既然想要静态变量. 那直接定义成一个静态变量不就得了么?well. 以后你会需到很多场景是不能简单通过 定义 静态属性来实现的.比如:你想知道一个航班当前的状态.是到达了、延迟了、取消了、还是已经飞走了.想知道这种状态你必须经历以下几步:
-
- 连接航空公司API查询
- 对查询结果进行解析
- 返回结果给你的用户
- Python 属性方法航班查询(模拟属性方法作用)
class Flight(object):
def __init__(self,name):
self.flight_name = name
def checking_status(self):
print("checking flight %s status " % self.flight_name)
return 1
@property
def flight_status(self):
status = self.checking_status()
if status == 0 :
print("flight got canceled...")
elif status == 1 :
print("flight is arrived...")
elif status == 2:
print("flight has departured already...")
else:
print("cannot confirm the flight status...,please check later")
f = Flight("CA980")
f.flight_status
现在只能查询航班状态. 目前flight_status已经是个属性了. 那么我给他赋个值呢?如下:
f = Flight("CA980")
f.flight_status
f.flight_status = 2
输出: 说不能更改这个属性.我擦.......怎么办怎么办.....?
C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe E:/Python_Engineering/Day_08/03_属性方法航班状态查询.py
checking flight CA980 status
flight is arrived...
Traceback (most recent call last):
File "E:/Python_Engineering_Review/Day_08/03_属性方法航班状态查询.py", line 28, in <module>
f.flight_status = 2
AttributeError: can't set attribute
Process finished with exit code 1
当然可以改. 不过需要通过@proerty.setter装饰器再装饰一下. 此时需要写一个新方法.对这个flight_status进行更改
- Python 属性方法航班查询(Perfect 属性方法)
class Flight(object):
def __init__(self, name):
self.flight_name = name
def checking_status(self):
print("checking flight %s status " % self.flight_name)
return 1
@property
def flight_status(self):
status = self.checking_status()
if status == 0:
print("flight got canceled...")
elif status == 1:
print("flight is arrived...")
elif status == 2:
print("flight has departured already...")
else:
print("cannot confirm the flight status...,please check later")
@flight_status.setter # 修改
def flight_status(self, status):
status_dic = {
1: "canceled",
2:"arrived",
3: "departured"
}
print("\033[31;1mHas changed the flight status to \033[0m", status_dic.get(status))
@flight_status.deleter # 删除
def flight_status(self):
print("status got removed...")
f = Flight("CA980")
f.flight_status
# 触发@flight_status.setter 可随意修改
f.flight_status = 2
del f.flight_status

浙公网安备 33010602011771号