#反射对象的属性
(==ret =getattr(a,<span >'<span >name<span >')<span >#<span >通过变量名的字符串形式取到的值<span >print(ret)#反射对象的方法ret =getattr(a,
#反射类的方法:
hasattr(A,)()#反射类的属性
=20(getattr(A,))#反射模块的属性
(getattr(my,))#反射自己模块中的变量(getattr(sys.modules[],],)()setattr设置/修改变量
<span >pass<span >setattr(A,
<span >'<span >name<span >',<span >'<span >alex<span >'<span >)<span >print(A,name)delattr删除变量
delattr(a,)2,__str__和__repr__改变对象的字符串显示__str__,__repr__
自定制格式化字符串__format__
== % str(self. =Teacher(,250(>> %r%nezha)l = [1,2,3,4,5] == = Classes((py_s9) (self): == open() a a#对象后面加括号,触发执行。
#注:构造方法的执行是由创建对象触发的,即:对象 = 类名() ;而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()
= k self.(k,self.= A()()
3,item系列__getitem__\__setitem__\__delitem__
===</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">def</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">__getitem__</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">(self,item): </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> hasattr(self,item): </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">return</span> self.<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">__dict__</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">[item]</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">def</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">__setitem__</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">(self,key,value): self.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">__dict__</span>[key] =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> value</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">def</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">__de<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>tem__</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">(self,key): </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">del</span> self.<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">__dict__</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">[key]
f
= Foo(<span >'<span >egon<span >',38,<span >'<span >男<span >'<span >)<span >print(f[<span >'<span >name<span >'<span >])
f[<span >'<span >hobby<span >'] = <span >'<span >男<span >'
<span >print(f.hobby,f[<span >'<span >hobby<span >'<span >])
<span >del f.hobby <span >#<span > object 原生支持 delattr
<span >del f[<span >'<span >hobby<span >'] <span >#<span > 通过自己实现的
<span >print(f.<span >dict)4,__new__
__init__ 初始化方法 __new__ 构造方法 : 创建一个对象= 1 ( (cls,*args,**( object.(A,**a1 =<span > A()
a2 =<span > A()
a3 =<span > A()
<span >print<span >(a1)
<span >print<span >(a2)
<span >print(a3)
单例模式# 一个类 始终 只有 一个 实例# 当你第一次实例化这个类的时候 就创建一个实例化的对象# 当你之后再来实例化的时候 就用之前创建的对象= == (cls,** cls. cls. = object. cls.egon = A(<span >'<span >egg<span >',38<span >)
egon.cloth = <span >'<span >小花袄<span >'<span >
nezha = A(<span >'<span >nazha<span >',25<span >)
<span >print<span >(nezha)
<span >print<span >(egon)
<span >print<span >(nezha.name)
<span >print<span >(egon.name)
<span >print(nezha.cloth)5,__eq__ =
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">def</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">__eq__</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">(self,other): </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">if</span> self.<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">__dict__</span> == other.<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">__dict__</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> True </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> False
ob1
= A(<span >'<span >egon<span >'<span >)ob2 = A(<span >'<span >egg<span >'<span >)
<span >print(ob1 == ob2)
#'=='默认比较内存地址
6,__hash__class A:
7,hashlib#提供摘要算法hashlibmd5 =(md5.hexdigest())
1,不管算法多么不同,摘要的功能始终不变
2,对于相同的字符串使用同一个算法进行摘要,得到的值总是不变的
3,使用不同算法对相同的字符串进行摘要,得到的值应该不同
4,不管是用什么算法,hashlib的方式永远不变
# 摘要算法# 密码的密文存储# 文件的一致性验证 # 在下载的时候 检查我们下载的文件和远程服务器上的文件是否一致 # 两台机器上的两个文件 你想检查这两个文件是否相等= input(= input( line = line.split(=== usr == user md5_pwd ==()
#加盐
hashlib md5 = hashlib.md5(bytes(,enCoding=md5.update(b(md5.hexdigest())#动态加盐
hashlib md5 = hashlib.md5(bytes(,enCoding=)+bmd5.update(b(md5.hexdigest())#文件一致性的校验
=