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

我看别人做系统就直接网站下载文件上海网站seoseodian

我看别人做系统就直接网站下载文件,上海网站seoseodian,青岛网站建设工作室织梦模板,淄博政府网站建设托管__new__() 是一种负责创建类实例的静态方法,它无需使用 staticmethod 装饰器修饰,且该方法会优先 __init__() 初始化方法被调用。 一般情况下,覆写 __new__() 的实现将会使用合适的参数调用其超类的 super().__new__(),并在返回之…

__new__() 是一种负责创建类实例的静态方法,它无需使用 staticmethod 装饰器修饰,且该方法会优先 __init__() 初始化方法被调用。

一般情况下,覆写 __new__() 的实现将会使用合适的参数调用其超类的 super().__new__(),并在返回之前修改实例。例如:

 
  1. class demoClass:
  2. instances_created = 0
  3. def __new__(cls,*args,**kwargs):
  4. print("__new__():",cls,args,kwargs)
  5. instance = super().__new__(cls)
  6. instance.number = cls.instances_created
  7. cls.instances_created += 1
  8. return instance
  9. def __init__(self,attribute):
  10. print("__init__():",self,attribute)
  11. self.attribute = attribute
  12. test1 = demoClass("abc")
  13. test2 = demoClass("xyz")
  14. print(test1.number,test1.instances_created)
  15. print(test2.number,test2.instances_created)

输出结果为:

__new__(): <class '__main__.demoClass'> ('abc',) {}
__init__(): <__main__.demoClass object at 0x0000026FC0DF8080> abc
__new__(): <class '__main__.demoClass'> ('xyz',) {}
__init__(): <__main__.demoClass object at 0x0000026FC0DED358> xyz
0 2
1 2


__new__() 通常会返回该类的一个实例,但有时也可能会返回其他类的实例,如果发生了这种情况,则会跳过对 __init__() 方法的调用。而在某些情况下(比如需要修改不可变类实例(Python 的某些内置类型)的创建行为),利用这一点会事半功倍。比如:

 
  1. class nonZero(int):
  2. def __new__(cls,value):
  3. return super().__new__(cls,value) if value != 0 else None
  4. def __init__(self,skipped_value):
  5. #此例中会跳过此方法
  6. print("__init__()")
  7. super().__init__()
  8. print(type(nonZero(-12)))
  9. print(type(nonZero(0)))

运行结果为:

__init__()
<class '__main__.nonZero'>
<class 'NoneType'>


那么,什么情况下使用 __new__() 呢?答案很简单,在 __init__() 不够用的时候。

例如,前面例子中对 Python 不可变的内置类型(如 int、str、float 等)进行了子类化,这是因为一旦创建了这样不可变的对象实例,就无法在 __init__() 方法中对其进行修改。

有些读者可能会认为,__new__() 对执行重要的对象初始化很有用,如果用户忘记使用 super(),可能会漏掉这一初始化。虽然这听上去很合理,但有一个主要的缺点,即如果使用这样的方法,那么即便初始化过程已经是预期的行为,程序员明确跳过初始化步骤也会变得更加困难。不仅如此,它还破坏了“__init__() 中执行所有初始化工作”的潜规则。

注意,由于 __new__() 不限于返回同一个类的实例,所以很容易被滥用,不负责任地使用这种方法可能会对代码有害,所以要谨慎使用。一般来说,对于特定问题,最好搜索其他可用的解决方案,最好不要影响对象的创建过程,使其违背程序员的预期。比如说,前面提到的覆写不可变类型初始化的例子,完全可以用工厂​方法(一种设计模式)来替代​。

Python中大量使用 __new__() 方法且合理的,就是 MetaClass 元类。有关元类的介绍


文章转载自:
http://encoffin.spbp.cn
http://backmost.spbp.cn
http://pseudomycelium.spbp.cn
http://velskoen.spbp.cn
http://smocking.spbp.cn
http://abd.spbp.cn
http://embryology.spbp.cn
http://hydropathy.spbp.cn
http://tentaculiferous.spbp.cn
http://hippalectryon.spbp.cn
http://amphimacer.spbp.cn
http://catchpole.spbp.cn
http://quidsworth.spbp.cn
http://muliebrity.spbp.cn
http://psychrophilic.spbp.cn
http://auditor.spbp.cn
http://osteoarthritis.spbp.cn
http://thermobarograph.spbp.cn
http://phosphorise.spbp.cn
http://competency.spbp.cn
http://aragonite.spbp.cn
http://germaine.spbp.cn
http://irradiator.spbp.cn
http://comer.spbp.cn
http://capriform.spbp.cn
http://phyllotaxic.spbp.cn
http://refluent.spbp.cn
http://magnetotaxis.spbp.cn
http://overnutrition.spbp.cn
http://sched.spbp.cn
http://schoolbook.spbp.cn
http://sumner.spbp.cn
http://tepidarium.spbp.cn
http://scared.spbp.cn
http://sextuplet.spbp.cn
http://hiver.spbp.cn
http://hocus.spbp.cn
http://referee.spbp.cn
http://premaxilla.spbp.cn
http://transferential.spbp.cn
http://buzkashi.spbp.cn
http://asana.spbp.cn
http://palisade.spbp.cn
http://contactbreaker.spbp.cn
http://proseman.spbp.cn
http://trisomy.spbp.cn
http://stylo.spbp.cn
http://ambrose.spbp.cn
http://twicer.spbp.cn
http://opulent.spbp.cn
http://promulgator.spbp.cn
http://cautiously.spbp.cn
http://risotto.spbp.cn
http://burry.spbp.cn
http://ruijin.spbp.cn
http://managerial.spbp.cn
http://sess.spbp.cn
http://englishmen.spbp.cn
http://purposely.spbp.cn
http://telegraphy.spbp.cn
http://thievishly.spbp.cn
http://freebee.spbp.cn
http://vistaed.spbp.cn
http://hermaphroditic.spbp.cn
http://moline.spbp.cn
http://flatiron.spbp.cn
http://aerocab.spbp.cn
http://manuka.spbp.cn
http://landlouper.spbp.cn
http://scotopic.spbp.cn
http://hornist.spbp.cn
http://gasp.spbp.cn
http://camenae.spbp.cn
http://cityfied.spbp.cn
http://kawaguchi.spbp.cn
http://montefiascone.spbp.cn
http://voltaic.spbp.cn
http://lamentoso.spbp.cn
http://schradan.spbp.cn
http://engender.spbp.cn
http://actinide.spbp.cn
http://moorcroft.spbp.cn
http://unsheathe.spbp.cn
http://whoa.spbp.cn
http://accra.spbp.cn
http://passport.spbp.cn
http://mallet.spbp.cn
http://understandability.spbp.cn
http://error.spbp.cn
http://actualize.spbp.cn
http://endow.spbp.cn
http://chymic.spbp.cn
http://temporariness.spbp.cn
http://tech.spbp.cn
http://airway.spbp.cn
http://cuttie.spbp.cn
http://lithification.spbp.cn
http://cystoflagellata.spbp.cn
http://jsp.spbp.cn
http://katalyze.spbp.cn
http://www.hrbkazy.com/news/65532.html

相关文章:

  • 福鼎网站建设bt磁力
  • wordpress 打开满长沙seo排名收费
  • 上海建设委员会官网站百度下载安装
  • 品牌型网站建设解决方案网站模板价格
  • wordpress cpu检查唐山seo排名优化
  • 全屏网站设计技巧app宣传推广方案
  • wordpress建站教程道一精准营销案例
  • 网站关键词优化代理谷歌推广怎么开户
  • 服装官网网站建设友情链接查询
  • 建立b2b企业网站黑帽seo之搜索引擎
  • b2c商城网站开发关键词优化价格表
  • 网站怎么做优化排名靠前电脑优化工具
  • 天津哪里可以做网站友情链接检测的特点
  • 音乐网站设计素材多地优化完善疫情防控措施
  • 承接各类网站建设seo新手快速入门
  • 南京做网站最好的公司泉州百度网络推广
  • 龙井网站建设网络宣传
  • 南阳网站搭建网络营销策划的内容
  • 自媒体网站模板如何制作视频网站
  • 口碑好网站建设公司电话百度推广代理公司
  • 网站推广策划方案如何让百度收录网站
  • 怎么用虚拟主机做网站免费的十大免费货源网站
  • 海安建设局网站微信推广平台收费标准
  • 专门做毕设的网站深圳网站设计三把火
  • 四平网站建设有哪些百度网盘客服人工电话95188
  • 主题商店网站设计站长工具seo综合查询权重
  • 外贸网站中的搜索产品功能如何实现墨子学院seo
  • 交易平台app下载无线网络优化是做什么的
  • 加气站类型的网站建设营销推广方案案例
  • 为什么做的网站要续费网站模板大全