7.5.4 嵌套
-
函数:参数可以试任意类型
-
字典:对象和类都可以做字典的key和value
-
继承关系的查找关系
class School(object):
def __init__(self,title,addr):
self.title = title
self.address = addr
class ClassRoom(object):
def __init__(self,name,school_object):
self.name = name
self.school = school_object
s1 = School('北京','沙河')
s2 = School('上海','浦东')
s3 = School('深圳','南山')
c1 = ClassRoom('全栈21期',s1)
c1.name
c1.school.title
c1.school.address
# ############################################
v = [11,22,33,{'name':'山海','addr':'浦东'}]
v[0]
v[3]['name']
class Foo:
def __init__(self,num):
self.num = num
cls_list = []
for i in range(10):
cls_list.append(Foo)
for i in range(len(cls_list)):
obj = cls_list[i](i)
print(obj.num)
class Foo:
def f1(self):
print('f1')
def f2(self):
print('f2')
obj = Foo()
v = [ obj.f1,obj.f2 ]
for item in v:
item()
#当做参数传递
class Foo:
pass
class Foo(object):
pass
# 在python3中这俩的写法是一样,因为所有的类默认都会继承object类,全部都是新式类。
# 如果在python2中这样定义,则称其为:经典类
class Foo:
pass
# 如果在python2中这样定义,则称其为:新式类
class Foo(object):
pass
class Base(object):
pass
class Bar(Base):
pass
class School(object):
def __init__(self,title):
self.title = title
def rename(self):
pass
class Course(object):
def __init__(self,name,school_object):
self.name = name
self.school = school_object
def reset_price(self):
pass
class Classes(object):
def __init__(self,cname,course_object):
self.cname = cname
self.course = course_object
def sk(self):
pass
s1 = School('北京')
c1 = Course('Python',s1)
c2 = Course('Go',s1)
cl1 = Classes('全栈1期',c1)
#类似想要多个元素组合在一起,应该要一级一级来
class StackConfig(object):
list_display = '李邵奇'
def changelist_view(self):
print(self.list_display)
class UserConfig(StackConfig):
list_display = '利奇航'
class AdminSite(object):
def __init__(self):
self._register = {}
def registry(self,key,arg=StackConfig):
self._register[key] = arg
def run(self):
for key,value in self._register.items():
obj = value()
obj.changelist_view()
site = AdminSite()
site.registry(1)
site.registry(2,StackConfig)
site.registry(3,UserConfig)
site.run()
#对象和类在字典中的应用
7.6 特殊成员
7.6.1 init
class Foo:
def __init__(self,name):
self.name = name
obj = Foo('liujia')
7.6.2 new
class Foo:
def __init__(self,name):
self.name = name
def __new__(cls,*args,**kwargs):
"""
用于创建空对象,构造方法
"""
return object.__new__(cls)
obj = Foo()
#object,它new了一个对象出来
7.6.3 cal
class Foo:
def __call__(self,*args,**kwargs):
print(123)
obj = Foo()
obj()
#call的作用:这一步就能等于上面两步Foo()()
7.6.4 getitem setitem delitem
class Foo(object):
def __setitem__(self, key, value):
pass
def __getitem__(self, item):
return item + 'uuu'
def __delitem__(self, key):
pass
#实现字典里的方法
obj1 = Foo()
obj1['k1'] = 123 # 内部会自动调用 __setitem__方法,添加
val = obj1['xxx'] # 内部会自动调用 __getitem__方法,索引
print(val)
del obj1['ttt'] # 内部会自动调用 __delitem__ 方法,删除
7.6.5 str
class Foo:
def __str__(self):
"""
只有在打印对象的时候,会自动调用此方法,并将其返回值显示在页面
"""
obf = Foo()
print(obj)
class User(object):
def __init__(self,name,email):
self.name = name
self.email = email
def __str__(self):
return "%s %s" %(self.name,self.email,)
user_list = [User('二狗','2g@qq.com'),User('二蛋','2d@qq.com'),User('狗蛋','xx@qq.com')]
for item in user_list:
print(item)
#打印出来的不再是函数的地址,而是__str__的返回值
7.6.6 dict
class Foo(object):
def __init__(self,name,age,email):
self.name = name
self.age = age
self.email = email
obj = Foo('alex',19,'xxxx@qq.com')
print(obj)
print(obj.name)
print(obj.age)
print(obj.email)
val = obj.__dict__ # 去对象中找到所有变量并将其转换为字典
print(val)
7.6.7 上下文管理 enter exit
class Foo:
def __enter__(self):
self.x = open('a.txt',mode='a',encoding 'utf-8')
return self.x #return的值赋给了下面的ff
def __exit__(self,exc_type,exc_val.exc_tb):
self.x.close()
with Foo() as ff:
ff.write('liujia')
class Foo(object):
def do_something(self):
print('内部执行')
class Context:
def __enter__(self):
print('进入')
return Foo()
def __exit__(self, exc_type, exc_val, exc_tb):
print('退出')
with Context() as ctx:
print('内部执行')
ctx.do_something()
#总结,就是在执行某个函数之前,先执行一个程序,在执行另一个程序
7.6.8 两个对象相加 add
val = 5 + 8
print(val)
val = "alex" + "sb"
print(val)
class Foo(object):
def __add__(self, other):
return 123
obj1 = Foo()
obj2 = Foo()
val = obj1 + obj2
print(val)
#加减乘除都可以,类似的