标签:www def think 回收 red 关闭 嵌套循环 restore 使用
class A:pass
a = A()
print(isinstance(a, A))
# 结果呈现
True
class A:pass
class B(A):pass
print(issubclass(B, A))
print(issubclass(A, B))
# 结果呈现
True
False
四个可以实现自省的函数,下列方法适用于类和对象(一切皆对象,类本身也是一个对象)
hasattr
`````
def hasattr(*args, **kwargs): # real signature unknown
"""
Return whether the object has an attribute with the given name.
This is done by calling getattr(obj, name) and catching AttributeError.
"""
pass
- getattr
def getattr(object, name, default=None): # known special case of getattr
"""
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, ‘y‘) is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn‘t
exist; without it, an exception is raised in that case.
"""
pass
- setattr
def setattr(x, y, v): # real signature unknown; restored from doc
"""
Sets the named attribute on the given object to the specified value.
setattr(x, ‘y‘, v) is equivalent to ``x.y = v‘‘
"""
pass
- delattr
def delattr(x, y): # real signature unknown; restored from doc
"""
Deletes the named attribute from the given object.
delattr(x, ‘y‘) is equivalent to ``del x.y‘‘
"""
pass
- 四个方法的使用演示
name = 1
eval("print(name)") # 安全隐患
class A:
def func(self):
print("in func")
a = A()
a.name = "alex"
ret = getattr(a,"name") # 通过变量名的字符串形式取到的值
print(ret)
print(a.__dict__)
variable_name = input(">>>")
class A:
def func(self):
print("in func")
a = A()
a.name = "alex"
a.func()
ret = getattr(a,"func")
ret()
class A:
price = 20
@classmethod
def func(cls):
print("in func")
print(getattr(A,"price"))
if hasattr(A,"func"):
getattr(A, "func")()
import test_project.my_module_obj
print(getattr(test_project.my_module_obj,"day"))
getattr(test_project.my_module_obj,"wahaha")()
def qqxing():
print("qqxing")
year = 2018
import sys
print(getattr(sys.modules["main"],"year"))
getattr(sys.modules["main"],"qqxing")()
varible_name = input(">>>")
print(getattr(sys.modules[name], varible_name))
import time
print(time.strftime("%Y-%m-%d %H:%M:%S"))
print(getattr(time, "strftime")("%Y-%m-%d %H:%M:%S"))
import test_project.my_module_obj
print(getattr(test_project.my_module_obj,"C")())
class A:
pass
a = A()
setattr(A,"name","alex")
setattr(a,"name","nezha")
print(A.name)
print(a.name)
delattr(a,"name")
print(a.name)
delattr(A,"name")
print(A.name)
day = "Monday" # 周一
def wahaha():
print("wahahaha")
class C:
pass
## 3 __str__和__repr__
- 改变对象的字符串显示__str__,__repr__
- 自定制格式化字符串__format__
format_dict={
‘nat‘:‘{obj.name}-{obj.addr}-{obj.type}‘,#学校名-学校地址-学校类型
‘tna‘:‘{obj.type}:{obj.name}:{obj.addr}‘,#学校类型:学校名:学校地址
‘tan‘:‘{obj.type}/{obj.addr}/{obj.name}‘,#学校类型/学校地址/学校名
}
class School:
def init(self,name,addr,type):
self.name=name
self.addr=addr
self.type=type
def __repr__(self):
return ‘School(%s,%s)‘ %(self.name,self.addr)
def __str__(self):
return ‘(%s,%s)‘ %(self.name,self.addr)
def __format__(self, format_spec):
# if format_spec
if not format_spec or format_spec not in format_dict:
format_spec=‘nat‘
fmt=format_dict[format_spec]
return fmt.format(obj=self)
s1=School(‘myschool‘,‘北京‘,‘私立‘)
print(‘from repr: ‘,repr(s1))
print(‘from str: ‘,str(s1))
print(s1)
‘‘‘
str函数或者print函数--->obj.__str__()
repr或者交互式解释器--->obj.__repr__()
如果__str__没有被定义,那么就会使用__repr__来代替输出
注意:这俩方法的返回值必须是字符串,否则抛出异常
‘‘‘
print(format(s1,‘nat‘))
print(format(s1,‘tna‘))
print(format(s1,‘tan‘))
print(format(s1,‘asfdasdffd‘))
class A:
def str(self):
return "A‘s object"
def func(self):
return "wahaha"
a = A()
print(str(a)) # a.__str__ --> object
print(a) # 答应一个对象的时候,就是调用a.__str__
lst = [1, 2, 3, 4, 5] # 实例化一个列表类的对象
print(lst)
print(‘%s:%s‘ % ("A", a))
class Teacher:
def init(self, name, salary):
self.name = name
self.salary = salary
def str(self):
return "Teacher‘s object : %s" % self.name
def repr(self):
return str(self.__dict__)
def func(self):
return "wahaha"
nezha = Teacher("哪吒", 250)
print(nezha)
print(repr(nezha))
print(">>> %r" % nezha)
- %r repr() 实际上调的是 __repr__
- repr 是 str 的备胎,但str不能做repr的备胎
- print(obj) / ‘%s‘%obj / str(obj)的时候,实际上是内部调用了 obj.__str__方法,如果str方法有,那么它返回的必定是一个str数据类型
- 如果没有__str__方法,会先找本类中的 __repr__方法,再没有再找父类的 __str__
- repr(),只会找 __repr__, 如果没有找父类的 __repr__
## 4 __del__
- 析构方法,当对象在内存中被释放时,自动触发执行。
- 注:此方法一般无须定义,因为Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给Python解释器来执行,所以,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的。
class A:
def del(self): # 析构函数:在删除一个对象前进行一些收尾工作
print("执行我啦")
a = A()
del a # del 先执行了这个方法,又删除了变量 - - 执行我啦
print(a) # NameError: name ‘a‘ is not defined
执行我啦
File "C:/Users/thinkpad/Envs/daily_test/test_project/面向对象.py", line 1435, in
class A:
def del(self): # 析构函数:在删除一个对象前进行一些收尾工作
print("执行我啦")
a = A()
import time
time.sleep(3) # 执行了del 方法 - - 执行我啦
执行我啦
class A:
def del(self): # 析构函数
self.f.close() # 在 a.f 在内存中消失前,先关闭打开的文件
a = A()
a.f = open() # 打开文件 ,在操作系统中打开了一个文件,拿到了文件操作符存在了内存中
del a # 删除 a 以后,a.f 拿到了的文件操作符也消失在内存中
## 5 item系列
- __getitem__\__setitem__\__delitem__
class Foo:
def init(self,name):
self.name=name
def __getitem__(self, item):
print(self.__dict__[item])
def __setitem__(self, key, value):
self.__dict__[key]=value
def __delitem__(self, key):
print(‘del obj[key]时,我执行‘)
self.__dict__.pop(key)
def __delattr__(self, item):
print(‘del obj.key时,我执行‘)
self.__dict__.pop(item)
f1=Foo(‘sb‘)
f1[‘age‘]=18
f1[‘age1‘]=19
del f1.age1
del f1[‘age‘]
f1[‘name‘]=‘alex‘
print(f1.__dict__)
## 6 __new__
- 一个类 始终 只有 一个 实例
- 当你第一次实例化这个类的时候,就创建一个实例化的对象
- 当你之后再来实例化的时候,就用之前创建的对象
class A:
def init(self):
self.x = 1
print(‘in init function‘)
def new(cls, *args, **kwargs):
print(‘in new function‘)
return object.__new__(A, *args, **kwargs)
a = A()
print(a.x)
class Singleton:
def new(cls, *args, **kw):
if not hasattr(cls, ‘_instance‘):
cls._instance = object.new(cls, *args, **kw)
return cls._instance
one = Singleton()
two = Singleton()
two.a = 3
print(one.a)
print(id(one))
print(id(two))
print(one == two)
print(one is two)
## 7 __call__
- 对象后面加括号,触发执行。
- 注:构造方法的执行是由创建对象触发的,即:对象 = 类名() ;而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()
class A:
def init(self,name):
self.name = name
def call(self, *args, **kwargs):
"""
打印这个对象中的所有属性
:param args:
:param kwargs:
:return:
"""
print("执行我啦")
for k in self.__dict__:
print(k,self.__dict__[k])
a = A("alex")
a()
a = A("alex")()
执行我啦
name alex
执行我啦
name alex
## 8 __len__
class Classes:
def init(self, name):
self.name = name
self.student = []
def len(self):
return len(self.student)
py_ss = Classes("python全栈9期")
py_ss.student.append("二哥")
py_ss.student.append("泰哥")
print(len(py_ss))
2
## 9 __hash__
class A:
def init(self,name,sex):
self.name = name
self.sex = sex
def hash(self):
return hash(self.name + self.sex)
a = A("agg", "男")
b = A("agg", "男")
print(hash(a)) # 2067652227830744138
print(hash(b)) # 2067652227830744138
c = A("agg", "女")
print(hash(c)) # 2005968340684947333
## 10 __eq__
class A:
def init(self,name):
self.name = name
obj1 = A("egg")
obj2 = A("egg")
print(obj1 == obj2) # False
class A:
def init(self,name):
self.name = name
def eq(self, other):
if self.name == other.name:
return True
else:
return False
obj1 = A("egg")
obj2 = A("egg")
obj3 = A("egge")
print(obj1 == obj2) # True
print(obj1 == obj3) # False
## 11 例1 纸牌游戏
from collections import namedtuple
Card = namedtuple("Card", ["rank", "suit"]) # rank 牌面大小 suit牌面花色
class FranchDeck:
ranks = [str(n) for n in range(2,11)] + list(‘JQKA‘)
suits = [‘红心‘,‘方板‘,‘梅花‘,‘黑桃‘]
def __init__(self):
self._cards = [Card(rank,suit) for rank in FranchDeck.ranks
for suit in FranchDeck.suits] # 列表表达式 嵌套循环匹配获取所有牌
def __len__(self): # 获取牌数量
return len(self._cards)
def __getitem__(self, item):
return self._cards[item]
def __setitem__(self, key, value):
self._cards[key] = value
def __str__(self):
return json.dumps(self._cards,ensure_ascii=False) # json 序列化
deck = FranchDeck()
print(deck[0]) # 第一张牌
from random import choice
print(choice(deck)) # 随机抽牌
print(choice(deck)) # 随机抽牌
from random import shuffle
shuffle(deck) # 洗牌
print(deck._cards[:5]) # 获取前5 个
print(deck[:5]) # 获取前5 个 item方法
import json
print(deck) # 查看所有牌面
## 12 例2 100个对象 名字和性别和年龄不同
class Person:
def init(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def __hash__(self):
return hash(self.name+self.sex)
def __eq__(self, other):
if self.name == other.name and self.sex == other.sex:return True
p_lst = []
for i in range(10):
p_lst.append(Person(‘egon‘,i,‘male‘))
print(p_lst)
print(set(p_lst))
````
标签:www def think 回收 red 关闭 嵌套循环 restore 使用
原文地址:https://www.cnblogs.com/xiaoqshuo/p/9778261.html