翻译:一个类方法。
classmethod是一个装饰器,可以装饰给类内部的方法,使该方法绑定给类来使用。
对象的绑定方法特殊之处
---由对象来调用,会将对象当作第一个参数传给该方法。
类的绑定方法特殊之处
---由类来调用,会将类当作第一个参数传给该方法。
class People:
def __init__(self,name,age):
self.name=name
self.age=age
@classmethod
def tell_info(cls):
print(cls)
print('此处是类方法...')
pass
p=People('tank',18)
p.tell_info() #<class '__main__.People'>
#此处是类方法...
People.tell_info()#<class '__main__.People'>
#此处是类方法...
也是一个装饰器,可以装饰给类内部的方法,使该方法既不绑定给对象,也不绑定给类。
import hashlib
import uuid
import settings
class Teacher:
def __init__(self,user,pwd):
self.user=user
self.pwd=pwd
#主页
def index(self):
if self.user=='tank' and self.pwd=='123':
print()
@classmethod
def login_auth_from_settinfs(cls):
obj=cls(settings.USER,settings.PWD)
return obj
@staticmethod
def create_id():
#生成一个唯一的id字符
uuid_obj=uuid.uuid4()
m=hashlib.md5()
m.update(str(uuid_obj).encode('utf8'))
return m.hexdigest()
obj=Teacher.login_auth_from_settinfs()
obj.index()
tea1=Teacher('tank','123')
tea1.index()
#import uuid 是一个加密模块,uuid通过时间戳生成一个世界上唯一的字符串
if __name__=='__main__':
print(type(uuid.uuid4()))
print(Teacher.create_id())
tea1=Teacher('tank','123')
print(tea1.create_id())
Python内置的函数,可以传入两个参数,用于判断参数1是否是参数2的一个实例。比如判断一个对象是否是一个类的实例
__class__
:对象的属性,获取该对象当前的类。
Python内置的函数,可以传入两个参数,用于判断参数1是否是参数2的子类。判断一个类是否是另一个类的子类。
class Foo:
pass
class Goo(Foo):
pass
foo_obj=Foo()
print(isinstance(foo_obj,Foo)) #True
print(isinstance(foo_obj,Goo)) #False
print(issubclass(Goo,Foo)) #True
3.--反射
指的是通过“字符串”对 对象或类的属性进行操作。
--hasatter:判断字符串是否是对象或类的属性。
--getatter:通过字符串,获取对象或类的属性。
--setatter:通过字符串,设置对象或类的属性。
--delatter:通过字符串,删除对象或类的属性。
class People:
country='China'
def __init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
#普通方式
p=People('tank',17,'male')
print('name' in p.__dict__) #是否在对象的名称空间-->true
print('country'in People.__dict__) #是否在类的名称空间 -->true
print('name'in People.__dict__) ##-->False
#hasattr
print(hasattr(p,'name')) #True
print(hasattr(People,'country')) #True
#普通方式 get用法是有则保留,没则赋值
print(p.__dict__.get('name')) #tank
print(p.__dict__.get('level','9')) #9
#getattr
print(getattr(p,'name','jason')) #tank
print(getattr(p,'name1','jason')) #jason
print(getattr(People,'country2','zhongguo')) #zhongguo
#setattr
p.leval=10
print(p.leval) #10
#反射
setattr(p,'sal','3.0')
print(hasattr(p,'sal')) #True
#delattr
#普通
del p.leval
print(hasattr(p,'level')) #False
#反射
delattr(p,'sal')
print(hasattr(p,'sal')) #False
'''反射小练习'''
class Movie:
def input_cmd(self):
print('输入命令:')
while True:
cmd=input('请输入执行的方法名:').strip()
#若用户输入的cmd命令是当前对象的属性
if hasattr(self,cmd):
method=getattr(self,cmd)
method()
break
else:
print('命令错误,请重新输入')
def upload(self):
print('电影上传')
def download(self):
print('电影下载')
movie_obj=Movie()
movie_obj.input_cmd()
运行结果:
输入命令:
请输入执行的方法名:upload
电影上传
or
输入命令:
请输入执行的方法名:download
电影下载
凡是在类内部定义,以__
开头__
结尾的方法都称之为魔法方法,又称”类的内置方法“
__init__
:会在调用类时触发
__str__
:会在打印对象时触发
__del__
:对象被销毁前执行该方法,该方法会在最后执行
__getattr__
:会在对象.属性时,“属性没有”的情况下C才会触发。
__setattr__
:会在对象“对象.属性=属性值”时触发。
__call__
:会在对象被调用时触发。
__new__
:会在__init__
执行前触发
class MyFile(object):
def __init__(self,file_name,mode='r',encoding='utf8'):
self.file_name=file_name
self.mode=mode
self.encoding=encoding
def file_open(self):
self.f=open(self.file_name,self.mode,encoding=self.encoding)
def file_read(self):
res=self.f.read()
print(f'''
当前文件名称:{self.file_name}
当前文件数据:{res}
''')
def __del__(self):
self.f.close()
print('文件关闭成功!')
f=MyFile('jason雨后的小故事.txt')
f.file_open()
f.file_read()
print('程序结束,对象被销毁!')
运行结果:
当前文件名称:jason雨后的小故事.txt
当前文件数据:标题: jason雨后的小故事
文本: 啊,今天天气真的很好啊,我要去开车车啊...
程序结束,对象被销毁!
文件关闭成功!
单例模式指的是单个实例,实例指的是调用类产生的对象
实例化多个对象会产生不同的内存地址,单例可以让所有调用者,在调用类产生对象的情况下都指向同一份内存地址。例如打开文件。
单例的目的:为了减少内存的占用。
原文:https://www.cnblogs.com/lidandanaa/p/11663312.html