Python学习笔记:day21 类嵌套 & 特殊方法
2021/6/10 1:21:19
本文主要是介绍Python学习笔记:day21 类嵌套 & 特殊方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
今日内容
- 嵌套
- 特殊成员:__ init __
- type/isinstance/issubclass/super
- 异常处理
内容回顾
def login(): pass login()
class Account: def login(self): pass obj = Account() obj.login()
-
谈谈你了解的面向对象?
-
类和对象是什么关系?对象是类的一个实例。
class Foo: def __ini__(self,name): def run(self): pass obj1 = Foo('alex') obj2 = Foo('eric')
-
self是什么?
#self就是一个形式参数,对象调用方法时,python内部会将该对象传给这个参数 class Foo: def run(self,num): pass boj = Foo() obj.run(5)
-
类成员&对象成员 以及他们之间的关系
class Foo: name = 'alex' def run(self): pass obj = Foo()
-
类、方法、对象 都可以当作变量或嵌套到其他类型中
class Foo: def run(self): pass v = [Foo,Foo] v = [Foo(),Foo()] obj = Foo() v = [obj.run,obj.run.obj.run]
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_proce(self): pass class Classes(object): def __init__(self,cname,course_object): self.cname = cname self.course_object = course_object def sk(self): pass s1 = School('北京') c1 = Course('Python',s1) c2 = Course('Go',s1) cl1 = Classes('全栈1期',c1)
内容详细
1.嵌套
-
函数:参数可以是任意类型。
-
字典:对象和类都可以做字典的key和value
-
继承的查找关系
class StarkConfig(object): pass class AdminSite(object): def __init__(self): self.data_list = [] def register(self,arg): self.data_list.append(arg) site = AdminSite() obj = StarkConfig() site.register(obj)
class StarkConfig(object): def __init__(self,name,age): self.name = name self.age = age class AdminSite(object): def __init__(self): self.data_list = [] self.sk = None def set_sk(self,arg): self.sk = arg site = AdminSite() #data_list = [] sk = None site.set_sk(StarkConfig) site.sk('alex',19)
class StackConfig(object): pass class Foo(object): pass class Base(object): pass class AdminSite(object): def __init__(self): self._register = {} def registry(self,key,arg): self._register[key] = arg site = AdminSite() site.registry(1,StackConfig) site.registry(2,StackConfig) site.registry(3,StackConfig) site.registry(4,Foo) site.registry(5,Base) for k,v in site._register.items(): print(k,v())
class StackConfig(object): list_display = 'lishaoqi' def changelist_view(self): print(self.list_display) class UserConfig(StackConfig): list_display = 'liqihang' 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() # print(key,obj.list_display) site = AdminSite() site.registry(1) site.registry(2,StackConfig) site.registry(3,UserConfig) site.run()
2.特殊成员
2.1初始化方法 __ init __
''' py文件的注释 ''' class Foo: ''' 类是干啥的... ''' def __init__(self,a1): ''' 初始化方法 :param a1: ''' self.a1 = a1 obj = Foo('alex')
2.2构造方法 __ new__
class Foo: def __init__(self): ''' 用于给对象赋值,初始化方法 ''' print('给对象中进行赋值') self.x = 123 def __new__(cls, *args, **kwargs): ''' 用于创建空对象,构造方法 :param args: :param kwargs: ''' print('创建对象') return object.__new__(cls) obj = Foo() print(obj)
2.3 __ cal __
class Foo(object): def __call__(self, *args, **kwargs): print('执行call方法') # obj = Foo() # obj() Foo()()
from wsgiref.simple_server import make_server def func(environ,start_response): start_response("200 OK",[('Content-Type','text/plain;charset=utf-8')]) return ['你好'.encode('utf-8')] class Foo(object): def __call__(self, environ,start_response): start_response("200 OK", [('Content-Type', 'text/html;charset=utf-8')]) return ['你<h1 style="color:red;">不好</h1>'.encode('utf-8')] #作用:写一个网站,用户只要来访问,就自动找到第三个参数并执行 server = make_server('127.0.0.1',8000,Foo()) server.serve_forever()
2.4 __ getitem __ __ setitem __ __ delitem __
class Foo(object): def __setitem__(self, key, value): print(key,value) 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__方法
2.5 __ str __
class Foo(object): def __str__(self): ''' 只有在打印对象时,会自动化调用此方法,并将其返回值在页面显示出来 :return: ''' return 'aefsfe' obj = Foo() print(obj,type(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('狗蛋','gd@qq.com')] for item in user_list: print(item)
2.6 __ dict __
class Foo(object): def __init__(self,name,age,email): self.name = name self.age = age self.email = email obj = Foo('alex',19,'xxx@qq.com') print(obj) print(obj.name) print(obj.age) print(obj.email) val = obj.__dict__ #去对象中找到所有变量并将其转换为字典 print(val)
2.7上下文管理【面试题】
class Foo(object): def __enter__(self): self.x = open('a.txt',mode='a',encoding='utf-8') return self.x def __exit__(self, exc_type, exc_val, exc_tb): self.x.close() with Foo() as ff: ff.write('alex') ff.write('alex') ff.write('alex') ff.write('alex') ff.write('alex')
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()
2.8两个对象相加
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)
特殊成员:就是为了能够快速实现执行某些方法而生。
3.内置函数补充
3.1 type,查看类型
class Foo: pass obj = Foo() if type(obj) == Foo: print('obj是Foo类的对象')
3.2 issubclass
class Base: pass class Base1(Base): pass class Foo(Base1): pass class Bar: pass print(issubclass(Bar,Base)) print(issubclass(Foo,Base))
3.3 isinstance
# class Base(object): # pass # class Foo(Base): # pass # class Bar: # pass # obj = Foo() # print(isinstance(obj,Foo)) #判断obj是否是Foo类或基类的实例(对象) # print(isinstance(obj,Base)) # print(isinstance(obj,Bar))
4. super
class Base(object): def func(self): print('base.func') return 123 class Foo(Base): def func(self): v1 = super().func() print('foo.func',v1) obj = Foo() obj.func() #supper().func()去父类中找func方法并执行
class Bar(object): def func(self): print('base.func') return 123 class Base(Bar): pass class Foo(Base): def func(self): v1 = super().func() print('foo.func',v1) obj = Foo() obj.func() #supper().func()根据类的继承关系,按照顺序挨个找func方法并执行(找到第一个就不再往后继续找)
class Base(object): #Base -> object def func(self): super().func() print('base.fun') class Bar(object): def func(self): print('bar.fun') class Foo(Base,Bar): #Foo -> Base -> Bar pass # obj = Foo() # obj.func() obj = Base() obj.func() #supper().func()根据self对象所属类的继承关系,按照顺序挨个找func方法并执行(找到第一个就不再往后继续找)
5.异常处理
5.1基本格式
try: pass except Exception as e: pass
try: # int('asdf') # v = [] # v[10000] pass except ValueError as e: pass except IndexError as e: pass except Exception as e: print(e) #e是Exception类的对象,中有一个错误信息 # int('asdf') #ValueError # v = [] # v[10000] #IndexError
try: # int('asdf') except Exception as e: print(e) #e是Exception类的对象,中有一个错误信息 finally: print('最后无论对错都会执行') ######特殊情况 def func(): try: #v = 1 #return 123 int('asdf') except Exception as e: print(e) #e是Exception类的对象,中有一个错误信息 finally: print('最后无论对错都会执行') func()
5.2主动触发异常
try: int('123') raise Exception('asfweasd') #代码中主动抛出异常 except Exception as e: print(e)
def func(): result = False try: with open('x.log',mode='r',encoding='utf-8') as f: data = f.read() if 'alex' not in data: raise Exception() except Exception as e: result = False return result
总结
- 特殊成员(**)
- 嵌套
- type/issubclass/isinstance
- super
- 异常
这篇关于Python学习笔记:day21 类嵌套 & 特殊方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-25Python编程基础:变量与类型
- 2024-11-25Python编程基础与实践
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器