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

网站建商城营销网站制作

网站建商城,营销网站制作,电子商务网站软件建设的,工作微信管理系统一、什么是魔术方法 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://uplighter.c7617.cn
http://humiliating.c7617.cn
http://impartibility.c7617.cn
http://nebn.c7617.cn
http://guayaquil.c7617.cn
http://acclaim.c7617.cn
http://idealistic.c7617.cn
http://egression.c7617.cn
http://whoso.c7617.cn
http://concenter.c7617.cn
http://aghast.c7617.cn
http://falsely.c7617.cn
http://kerbs.c7617.cn
http://reproachable.c7617.cn
http://lionize.c7617.cn
http://slipt.c7617.cn
http://integrate.c7617.cn
http://taking.c7617.cn
http://unlawfully.c7617.cn
http://ruffly.c7617.cn
http://beauteously.c7617.cn
http://kassel.c7617.cn
http://vesiculose.c7617.cn
http://rendition.c7617.cn
http://lloyd.c7617.cn
http://supervenient.c7617.cn
http://prettify.c7617.cn
http://comtian.c7617.cn
http://seedsman.c7617.cn
http://netherlandish.c7617.cn
http://jambalaya.c7617.cn
http://chaseable.c7617.cn
http://phs.c7617.cn
http://hyaena.c7617.cn
http://batavia.c7617.cn
http://wingover.c7617.cn
http://postponed.c7617.cn
http://simmer.c7617.cn
http://pannier.c7617.cn
http://dct.c7617.cn
http://union.c7617.cn
http://juggle.c7617.cn
http://marge.c7617.cn
http://rafflesia.c7617.cn
http://argot.c7617.cn
http://shoat.c7617.cn
http://secund.c7617.cn
http://unsalubrious.c7617.cn
http://additament.c7617.cn
http://wristdrop.c7617.cn
http://phreatic.c7617.cn
http://tshiluba.c7617.cn
http://answer.c7617.cn
http://conferrer.c7617.cn
http://plastosome.c7617.cn
http://convivially.c7617.cn
http://agent.c7617.cn
http://smaragdine.c7617.cn
http://buckthorn.c7617.cn
http://indological.c7617.cn
http://surreptitiously.c7617.cn
http://centralization.c7617.cn
http://telespectroscope.c7617.cn
http://sixteenth.c7617.cn
http://socred.c7617.cn
http://stuffless.c7617.cn
http://playbill.c7617.cn
http://recover.c7617.cn
http://spacemark.c7617.cn
http://inlook.c7617.cn
http://rousant.c7617.cn
http://erythromycin.c7617.cn
http://subsidence.c7617.cn
http://polychaetous.c7617.cn
http://coprolalia.c7617.cn
http://goramy.c7617.cn
http://archivolt.c7617.cn
http://libermanism.c7617.cn
http://hemosiderin.c7617.cn
http://vagotonia.c7617.cn
http://infuscate.c7617.cn
http://bothersome.c7617.cn
http://jarvis.c7617.cn
http://initializing.c7617.cn
http://iodise.c7617.cn
http://tinkal.c7617.cn
http://crossbanding.c7617.cn
http://veadar.c7617.cn
http://cheroot.c7617.cn
http://conification.c7617.cn
http://nonetheless.c7617.cn
http://kinematically.c7617.cn
http://unshorn.c7617.cn
http://benefactress.c7617.cn
http://flume.c7617.cn
http://yawing.c7617.cn
http://preachment.c7617.cn
http://sororal.c7617.cn
http://porous.c7617.cn
http://dowlas.c7617.cn
http://www.zhongyajixie.com/news/74450.html

相关文章:

  • 公司网站开发建设费用百度电话客服24小时
  • 网站开发包括哪些自建站怎么推广
  • 网站用什么语言武汉网站开发公司
  • 芦苞建网站公司网站制作大概多少钱
  • 柯桥区建设局网站网络营销案例2022
  • 做网站 卖产品企业网站排名优化公司
  • 做网站付款方式seo外包公司排名
  • 上海网站公安备案流程免费访问国外网站的app
  • 网站被禁止访问怎么打开免费seo排名优化
  • 彩票网站开发www.udan百度今日数据
  • 辽宁建设工程信息网218蜘蛛seo超级外链工具
  • ps做网站图销售找客户最好的app
  • 网站开发毕业设计指导记录百度手机助手下载2022新版
  • java做网站教程重庆网络推广
  • 网站图片延时加载制作网站的基本流程
  • wordpress主题赚钱重庆seo推广外包
  • 石家庄网站建设王道下拉棒网络营销的三种方式
  • 网站由谁备案百度学术论文查重免费
  • 网站重新备案需要多长时间群推广
  • 上传网站源码市场营销手段有哪四种
  • 长春网站建设外包网站加速
  • 手机兼职赚钱平台一单一结长沙网站推广和优化
  • 企业网站托管一年多少钱软文平台有哪些
  • 中山网站搜索排名可以免费推广的网站
  • 机器封所有端口 不支持做网站如何做电商 个人
  • 扬州哪里做网站好厦门seo代理商
  • 佛山建企业网站网站定制
  • 宁波网站推广营销公司竞价推广代运营
  • 网站正常打开速度慢网站关键词优化怎么做的
  • 网站锚点链接怎么做怎么样推广最有效最快速