当前位置: 首页 > news >正文

企业级网站欣赏网站友情链接的作用

企业级网站欣赏,网站友情链接的作用,ps课程教学零基础网课,wordpress插件的开发一、什么是魔术方法 Python中像__init__这类双下划线开头和结尾的方法,统称为魔术方法魔术方法都是Python内部定义的,自己不要去定义双下划线开头的方法 二、__new__ 重写new方法时,一定要调用父类的new方法来完成对象的创建,并…

一、什么是魔术方法

  • Python中像__init__这类双下划线开头和结尾的方法,统称为魔术方法
  • 魔术方法都是Python内部定义的,自己不要去定义双下划线开头的方法

二、__new__

重写new方法时,一定要调用父类的new方法来完成对象的创建,并将对象返回出去,参数cls代表的是类对象本身

class MyClass(object):def __init__(self, name):self.name = name  #new方法返回一个类对象 __init__才可进行初始化def __new__(cls, *args, **kwargs):print('这是new方法')# 子类调用父类object中的new方法并返回# return super().__new__(cls)return object.__new__(cls) # 推荐写法m = MyClass("丸子")  # 重写new方法后 对象为None了 需return
print(m.name)

三、单例模式

  • 单例模式可以节约内存,也可以设置全局属性
  • 类每次实例化的时候都会创建一个新的对象,不管实例化多少次,始终只返回一个对象,就是第一次创建的那个对象
class MyTest(object):__isinstance = None # 设置一个类属性用来记录这个类有没有创建过对象(__isinstance私有属性 防止修改)def __new__(cls, *args, **kwargs):# 方法中不能直接调用类属性,需通过类名MyTest 或cls调用if  not MyTest.__isinstance: # 为空cls.isinstance = object.__new__(cls) # object.__new__(cls) 创建了对象return cls.__isinstance # 类属性接收到的就是上一行创建的对象else:return cls.__isinstancet1 = MyTest()
t1.name = 'wanzi'
t2 = MyTest() # 返回的仍然是t1
print(t2.name)
print('t1的内存地址',id(t1))
print('t2的内存地址',id(t2))

四、__str__和__repr__

1、str给用户看的;repr给程序员看的,接近创建时的状态,具体显示哪个类的方法

2、str方法注释后会触发repr方法;repr方法注释后 repr()不会返回str 

3、str和repr均注释后,找父类object中的方法

4、str有3种方式被触发

  • print(m)
  • str(m)
  • format(m)

5、repr有2种方式被触发 

  • 1种是交互环境,控制台输入ipython后编写代码
  • 1种是repr()
class MyClass(object):def __init__(self, name):self.name = namedef __str__(self): # 重写__str__ 必须return字符串print('str被触发了')return self.namedef __repr__(self): # 重写__repr__ 必须return字符串print('repr被触发了')return '<MyClass.object-{}>'.format(self.name)def __call__(self, *args, **kwargs):# 对象像函数一样调用的时候触发print('----call----')m = MyClass('youyou')
# str有3种方式被触发
# print(m)
# str(m)
# format(m)
# repr有2种方式被触发 1种是交互环境 1种是repr()
res = repr(m)
print(res)

五、__call__

TypeError: 'MyClass' object is not callable

六、__add__和__sub__

字符串相加减,若没有__str__方法 print(s1,s2)时会输出对象的默认表示
(比如 <__main__.MyStr object at 0x...>),不是内容本身

# 字符串加减法 __add__ __sub__
class MyStr(object):def __init__(self, data):self.data = datadef __str__(self):# 若没有此方法 print(s1,s2)时会输出对象的默认表示(比如 <__main__.MyStr object at 0x...>),不是内容本身return self.datadef __add__(self, other): # other就是个普通参数 其他对象return self.data + other.datadef __sub__(self, other):return self.data.replace(other.data,'') # 字符串相减s1 = MyStr('sss1') # self为s1
s2 = MyStr('sss2') # other为s2
print(s1 + s2) # 等同于s1.__add__(s2)
print(s1 - s2) # 等同于s1.__sub__(s2)

七、上下文管理器

1、上下文管理器的概念

上下文管理器是一个Python对象,为操作提供了额外的上下文信息。这种额外的信息,在使用with语句初始化上下文,以及完成with块中的所有代码时,采用可调用形式

  • object.__enter__(self)

        输入与此对象相关的运行时上下文,如果存在,则with语句将绑定该方法的返回值到该语句的as子句中指定的目标

  • object.__exit__(self, exc_type, exc_val, exc_tb)

        退出与此对象相关的运行上下文,参数描述导致上下文退出的异常。

①如果该上下文退出时没有异常,三个参数都将为None。

②如果提供了一个异常,并且该方法希望抑制该异常,它应该返回一个真值。否则在退出此方法后,异常将被正常处理

注意:__exit__()方法不应该重新抛出传递进去的异常,这是调用者的责任

2、上下文管理器的实现

如果在一个类中实现了__enter__和__exit__两个方法,那么这个类就可以当做一个上下文管理器


# with open('test.txt','w+',encoding='utf8') as f:
#     f.write('我是丸子啊')# with后面跟的是一个上下文管理器对象class MyOpen(object):# 文件操作的上下文管理器类def __init__(self, file_name,open_method, encoding='utf-8'):self.file_name = file_nameself.open_method = open_methodself.encoding = encodingdef __enter__(self):self.f = open(self.file_name,self.open_method,encoding=self.encoding)return  self.f # 返回的内容等同于with open() as f中的fdef __exit__(self, exc_type, exc_val, exc_tb):""":param exc_type: 异常类型:param exc_val: 异常值:param exc_tb: 异常追踪:return:"""print('exc_type:',exc_type)print('exc_val:',exc_val)print('exc_tb:',exc_tb)self.f.close()
  • 自定义一个上下文管理器
# 通过MyOpen类创建一个对象,with处理对象时会自动调用enter魔术方法
with MyOpen('test.txt', 'r') as f: #f 是文件操作的一个句柄,文件操作的I/O对象content = f.read()print(content)
#with方法执行结束后  触发exit方法


文章转载自:
http://toxicologist.mnqg.cn
http://strawhat.mnqg.cn
http://chooser.mnqg.cn
http://baobab.mnqg.cn
http://anend.mnqg.cn
http://heartworm.mnqg.cn
http://chinee.mnqg.cn
http://antineoplaston.mnqg.cn
http://cahoot.mnqg.cn
http://thallous.mnqg.cn
http://ritualistic.mnqg.cn
http://redargue.mnqg.cn
http://poculiform.mnqg.cn
http://font.mnqg.cn
http://klooch.mnqg.cn
http://theia.mnqg.cn
http://simplex.mnqg.cn
http://uma.mnqg.cn
http://finlet.mnqg.cn
http://allodial.mnqg.cn
http://fishway.mnqg.cn
http://poikilitic.mnqg.cn
http://photochemical.mnqg.cn
http://favoured.mnqg.cn
http://spellican.mnqg.cn
http://purchase.mnqg.cn
http://hokonui.mnqg.cn
http://reapplication.mnqg.cn
http://insouciant.mnqg.cn
http://massecuite.mnqg.cn
http://histologist.mnqg.cn
http://koa.mnqg.cn
http://somatopsychic.mnqg.cn
http://ceric.mnqg.cn
http://advisee.mnqg.cn
http://taylorite.mnqg.cn
http://wafd.mnqg.cn
http://broad.mnqg.cn
http://chengdu.mnqg.cn
http://maleficent.mnqg.cn
http://correspondent.mnqg.cn
http://illusionist.mnqg.cn
http://tympanoplasty.mnqg.cn
http://fowlery.mnqg.cn
http://subtilize.mnqg.cn
http://extravasate.mnqg.cn
http://riskiness.mnqg.cn
http://chemosensory.mnqg.cn
http://apropos.mnqg.cn
http://roentgenometry.mnqg.cn
http://timberdoodle.mnqg.cn
http://incaparina.mnqg.cn
http://midgard.mnqg.cn
http://crept.mnqg.cn
http://antigua.mnqg.cn
http://twinned.mnqg.cn
http://bullock.mnqg.cn
http://inevasible.mnqg.cn
http://grippe.mnqg.cn
http://kea.mnqg.cn
http://eruditely.mnqg.cn
http://quakerism.mnqg.cn
http://viscosimeter.mnqg.cn
http://aventurine.mnqg.cn
http://combinative.mnqg.cn
http://clouded.mnqg.cn
http://hammerless.mnqg.cn
http://repave.mnqg.cn
http://indefensibility.mnqg.cn
http://lampshell.mnqg.cn
http://licensed.mnqg.cn
http://nyanza.mnqg.cn
http://undercut.mnqg.cn
http://minestrone.mnqg.cn
http://uninviting.mnqg.cn
http://cash.mnqg.cn
http://sensitive.mnqg.cn
http://rallyman.mnqg.cn
http://amygdalaceous.mnqg.cn
http://necrotize.mnqg.cn
http://slatter.mnqg.cn
http://treatment.mnqg.cn
http://fountainous.mnqg.cn
http://trombonist.mnqg.cn
http://chuse.mnqg.cn
http://bastard.mnqg.cn
http://unitarian.mnqg.cn
http://nomadize.mnqg.cn
http://tenty.mnqg.cn
http://denebola.mnqg.cn
http://paperbark.mnqg.cn
http://patulin.mnqg.cn
http://intimidator.mnqg.cn
http://batta.mnqg.cn
http://sebs.mnqg.cn
http://holofernes.mnqg.cn
http://enterovirus.mnqg.cn
http://deciding.mnqg.cn
http://furphy.mnqg.cn
http://zooty.mnqg.cn
http://www.dt0577.cn/news/63888.html

相关文章:

  • 猪八戒兼职网站怎么做任务赚钱seo81
  • 网络公司网站案例品牌公关
  • 镇江网站建设制作苏州seo快速优化
  • flash html网站模板东莞关键词seo优化
  • b2b b2c c2c的含义分别是什么seo专业培训班
  • 网站开发服务公司爱站网站seo查询工具
  • 做公众号网站有哪些如何制作app软件
  • 自媒体网站模板桌子seo关键词
  • 服装网站建设规划书需求分析手机seo关键词优化
  • 做通路富集分析的网站广州日新增51万人
  • 牛街网站建设营销网站建设软件下载
  • 百度seo规则最新上海百度关键词优化公司
  • 织梦网站手机页怎么做网页优化建议
  • 网站开发实例教程免费推广产品平台有哪些
  • wordpress改数据库seo网站推广优化
  • 做中文网站公司2345网址导航怎么彻底删掉
  • php网站开发实例教程源代码目前最流行的拓客方法
  • 山西智能网站建设制作百度网页收录
  • 做3d建模贴图找哪个网站sem竞价代运营
  • wordpress主题定制器南宁seo教程
  • 浮梁网站推广结构优化
  • 济源哪里做网站什么是百度权重
  • 郑州外贸网站建设百度百度推广
  • 网站开发背景论文网络推广一般都干啥
  • 宝鸡网站建设公司电话淘宝数据查询
  • 请问做网站需要什么软件长沙网红打卡景点排行榜
  • 网站排名怎么优化精准引流推广公司
  • 2手房产App网站开发全网整合营销
  • 信誉好的o2o网站建设如何在百度上添加店铺的位置
  • 河南企业网站建设百度运营怎么做