__new__: 对象的创建,是一个静态方法,第一个参数是cls。(想想也是,不可能是self,对象还没创建,哪来的self)
init : 对象的初始化, 是一个实例方法,第一个参数是self。call : 对象可call,注意不是类,是对象。先有创建,才有初始化。即先._init和call">
赞
踩
<section>
<article class="clearfix">
<p>__new__: 对象的创建,是一个静态方法,第一个参数是cls。(想想也是,不可能是self,对象还没创建,哪来的self)<br>
init : 对象的初始化, 是一个实例方法,第一个参数是self。
call : 对象可call,注意不是类,是对象。
先有创建,才有初始化。即先__new__,而后__init__。
上面说的不好理解,看例子。
1.对于__new__
- class Bar(object):
- pass
- class Foo(object):
- def __new__(cls, *args, **kwargs):
- return Bar()
- print Foo()
可以看到,输出来是一个Bar对象。
__new__方法在类定义中不是必须写的,如果没定义,默认会调用object.__new__去创建一个对象。如果定义了,就是override,可以custom创建对象的行为。
聪明的读者可能想到,既然__new__可以custom对象的创建,那我在这里做一下手脚,每次创建对象都返回同一个,那不就是单例模式了吗?没错,就是这样。可以观摩《飘逸的python - 单例模式乱弹》
定义单例模式时,因为自定义的__new__重载了父类的__new__,所以要自己显式调用父类的__new__,即object.__new__(cls, *args, **kwargs),或者用super()。,不然就不是extend原来的实例了,而是替换原来的实例。
2.对于__init__
使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例的时候。例如:
- # -*- coding: utf-8 -*-
- class Person(object):
- """Silly Person"""
- def __init__(self, name, age):
- self.name = name
- self.age = age
- def __str__(self):
- return '<Person: %s(%s)>' % (self.name, self.age)
- if __name__ == '__main__':
- piglei = Person('piglei', 24)
- print piglei
这样便是__init__最普通的用法了。但__init__其实不是实例化一个类的时候第一个被调用 的方法。当使用 Persion(name, age) 这样的表达式来实例化一个类时,最先被调用的方法 其实是 __new__ 方法。
3.对于__call__
对象通过提供__call__(slef, [,*args [,**kwargs]])方法可以模拟函数的行为,如果一个对象x提供了该方法,就可以像函数一样使用它,也就是说x(arg1, arg2...) 等同于调用x.__call__(self, arg1, arg2) 。模拟函数的对象可以用于创建防函数(functor) 或代理(proxy).
- class Foo(object):
- def __call__(self):
- pass
- f = Foo()#类Foo可call
- f()#对象f可call
总结,在Python中,类的行为就是这样,__new__、__init__、__call__等方法不是必须写的,会默认调用,如果自己定义了,就是override,可以custom。既然override了,通常也会显式调用进行补偿以达到extend的目的。
这也是为什么会出现"明明定义def _init__(self, *args, **kwargs),对象怎么不进行初始化"这种看起来诡异的行为。(注,这里_init__少写了个下划线,因为__init__不是必须写的,所以这里不会报错,而是当做一个新的方法_init__)
<!-- social network share -->
</section> <section style="height: auto !important;"> <div class="google-auto-placed" style="width: 100%; height: auto; clear: both; text-align: center;"><ins data-ad-format="auto" class="adsbygoogle adsbygoogle-noablate" data-ad-client="ca-pub-9051032955849697" data-adsbygoogle-status="done" style="display: block; margin: 10px auto; background-color: transparent; height: 0px;" data-ad-status="unfilled"><ins id="aswift_3_expand" style="display: inline-table; border: none; height: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 1130px; background-color: transparent;" tabindex="0" title="Advertisement" aria-label="Advertisement"><ins id="aswift_3_anchor" style="display: block; border: none; height: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 1130px; background-color: transparent; overflow: hidden; opacity: 0;"><iframe id="aswift_3" name="aswift_3" style="left: 0px; position: absolute; top: 0px; border: 0px; width: 1130px; height: 0px;" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="1130" height="0" frameborder="0" src="https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9051032955849697&output=html&h=280&adk=3038719801&adf=1265941513&pi=t.aa~a.4047818084~rp.1&w=1130&fwrn=4&fwrnh=100&lmt=1629105281&rafmt=1&to=qs&pwprc=5461822568&psa=1&format=1130x280&url=https%3A%2F%2Fwww.zhangshengrong.com%2Fp%2FYjNK4BPXW2%2F&flash=0&fwr=0&pra=3&rpe=1&resp_fmts=3&wgl=1&fa=40&dt=1629105280980&bpp=2&bdt=1357&idt=2&shv=r20210809&mjsv=m202108100101&ptt=9&saldr=aa&abxe=1&cookie=ID%3Ded90065e6e67172c-2247652deec80098%3AT%3D1621953625%3ART%3D1621953625%3AS%3DALNI_MZQh_3DjacCo60sRkH8eKPGQ_-rrg&prev_fmts=1130x200%2C1130x200%2C0x0&nras=2&correlator=1377094913137&frm=20&pv=1&ga_vid=847116081.1629105280&ga_sid=1629105280&ga_hid=1242984005&ga_fc=0&u_tz=480&u_his=1&u_java=0&u_h=720&u_w=1280&u_ah=680&u_aw=1280&u_cd=24&u_nplug=3&u_nmime=4&adx=67&ady=1988&biw=1263&bih=578&scr_x=0&scr_y=0&eid=20211866%2C31062297&oid=3&pvsid=705412846630679&pem=129&ref=https%3A%2F%2Fwww.google.com.hk%2F&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C1280%2C0%2C1280%2C680%2C1280%2C578&vis=1&rsz=%7C%7Cs%7C&abl=NS&fu=128&bc=31&ifi=4&uci=a!4&btvi=2&fsb=1&xpc=yz8xOTqovo&p=https%3A//www.zhangshengrong.com&dtd=24" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" data-google-container-id="a!4" data-google-query-id="CPGc24yatfICFQRfvAodZ4oLZQ" data-load-complete="true"></iframe></ins></ins></ins></div><header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/Ap1ZzMr10M/" rel="bookmark">python中的__init__ 、__new__、__call__小结</a> </h2> </header> <article class="clearfix"> <div class="summary"> 1.__new__(cls, *args, **kwargs) 创建对象时调用,返回当前对象的一个实例;注意:这里的第一个参数是cls即class本身2.__init__(self, *args, **kwargs) 创建完对象后调用,对当前对象的实例的一些初始化,无返回值,即在调用__new__之后,根据返回的实例初始化:注意,这里的第一个参数是self即对象本身[注意和new的区别]3.__call__(self, *args, **kwargs) 如果类实现了这个方法,相当于把这个类型 </div> </article> </section> <section> <header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/nDa9jgrNjb/" rel="bookmark">浅谈python中的__init__、__new__和__call__方法</a> </h2> </header> <article class="clearfix"> <div class="summary"> 前言 本文主要给大家介绍关于python中__init__.__new__和__call__方法的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍: 任何事物都有一个从创建,被使用,再到消亡的过程,在程序语言面向对象编程模型中,对象也有相似的命运:创建.初始化.使用.垃圾回收,不同的阶段由不同的方法(角色)负责执行. 定义一个类时,大家用得最多的就是 __init__ 方法,而 __new__ 和 __call__ 使用得比较少,这篇文章试图帮助大家把这3个方法的正确使用方式 </div> </article> </section> <!-- <h3>详解Python中的__new__、__init__、__call__三个特殊方法的其他相关文章</h3> --> <section style="height: auto !important;"> <div class="google-auto-placed" style="width: 100%; height: auto; clear: both; text-align: center;"><ins data-ad-format="auto" class="adsbygoogle adsbygoogle-noablate" data-ad-client="ca-pub-9051032955849697" data-adsbygoogle-status="done" style="display: block; margin: 10px auto 15px; background-color: transparent; height: 0px;" data-ad-status="unfilled"><ins id="aswift_4_expand" style="display: inline-table; border: none; height: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 1130px; background-color: transparent;" tabindex="0" title="Advertisement" aria-label="Advertisement"><ins id="aswift_4_anchor" style="display: block; border: none; height: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 1130px; background-color: transparent; overflow: hidden; opacity: 0;"><iframe id="aswift_4" name="aswift_4" style="left: 0px; position: absolute; top: 0px; border: 0px; width: 1130px; height: 0px;" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="1130" height="0" frameborder="0" src="https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9051032955849697&output=html&h=280&adk=3038719801&adf=1572198436&pi=t.aa~a.4047907623~rp.1&w=1130&fwrn=4&fwrnh=100&lmt=1629105281&rafmt=1&to=qs&pwprc=5461822568&psa=1&format=1130x280&url=https%3A%2F%2Fwww.zhangshengrong.com%2Fp%2FYjNK4BPXW2%2F&flash=0&fwr=0&pra=3&rpe=1&resp_fmts=3&wgl=1&fa=40&dt=1629105280985&bpp=1&bdt=1362&idt=1&shv=r20210809&mjsv=m202108100101&ptt=9&saldr=aa&abxe=1&cookie=ID%3Ded90065e6e67172c-2247652deec80098%3AT%3D1621953625%3ART%3D1621953625%3AS%3DALNI_MZQh_3DjacCo60sRkH8eKPGQ_-rrg&prev_fmts=1130x200%2C1130x200%2C0x0%2C1130x280%2C1263x578&nras=4&correlator=1377094913137&frm=20&pv=1&ga_vid=847116081.1629105280&ga_sid=1629105280&ga_hid=1242984005&ga_fc=0&u_tz=480&u_his=1&u_java=0&u_h=720&u_w=1280&u_ah=680&u_aw=1280&u_cd=24&u_nplug=3&u_nmime=4&adx=67&ady=2336&biw=1263&bih=578&scr_x=0&scr_y=300&eid=20211866%2C31062297&oid=3&pvsid=705412846630679&pem=129&ref=https%3A%2F%2Fwww.google.com.hk%2F&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C1280%2C0%2C1280%2C680%2C1280%2C578&vis=1&rsz=%7C%7Cs%7C&abl=NS&fu=128&bc=31&ifi=5&uci=a!5&btvi=3&fsb=1&xpc=RZUvVnc4WG&p=https%3A//www.zhangshengrong.com&dtd=515" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" data-google-container-id="a!5" data-google-query-id="CIPy-YyatfICFVMDXAodF_4IWg" data-load-complete="true"></iframe></ins></ins></ins></div><header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/On1voBwJay/" rel="bookmark">浅谈Python中函数的定义及其调用方法</a> </h2> </header> <article class="clearfix"> <div class="summary"> 一.函数的定义及其应用 所谓函数,就是把具有独立功能的代码块组织成为一个小模块,在需要的时候调用函数的使用包含两个步骤 1.定义函数–封装独立的功能 2.调用函数–享受封装的成果 函数的作用:在开发时,使用函数可以提高编写的效率以及代码的重用'' 函数: 函数是带名字的代码块,用于完成具体的工作 需要在程序中多次执行同一项任务时,你无需反复编写完成该任务的代码,而只需调用该任务的函数,让python运行其中的代码,你将发现,通过使用函数,程序编写,阅读,测试和修复都将更容易 1.定义函数 def </div> </article> </section> <section> <header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/wradKyB1BE/" rel="bookmark">浅谈python中的面向对象和类的基本语法</a> </h2> </header> <article class="clearfix"> <a href="https://www.zhangshengrong.com/p/wradKyB1BE/" class="thumbnail"> <img src="//zsrimg.ikafan.com/file_images/article/201606/20160613103121.jpg"> </a> <div class="summary"> 当我发现要写python的面向对象的时候,我是踌躇满面,坐立不安呀.我一直在想:这个坑应该怎么爬?因为python中关于面向对象的内容很多,如果要讲透,最好是用面向对象的思想重新学一遍前面的内容.这个坑是如此之大,犹豫再三,还是只捡一下重要的内容来讲吧,不足的内容只能靠大家自己去补充了. 惯例声明一下,我使用的版本是 python2.7,版本之间可能存在差异. 好,在开讲之前,我们先思考一个问题,看代码: 为什么我只创建是为 a 赋值,就可以使用一些我没写过的方法? 可能会有小伙伴说:因为 a </div> </article> </section> <section style="height: auto !important;"> <div class="google-auto-placed" style="width: 100%; height: auto; clear: both; text-align: center;"><ins data-ad-format="auto" class="adsbygoogle adsbygoogle-noablate" data-ad-client="ca-pub-9051032955849697" data-adsbygoogle-status="done" style="display: block; margin: 10px auto 15px; background-color: transparent; height: 0px;" data-ad-status="unfilled"><ins id="aswift_5_expand" style="display: inline-table; border: none; height: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 1130px; background-color: transparent;" tabindex="0" title="Advertisement" aria-label="Advertisement"><ins id="aswift_5_anchor" style="display: block; border: none; height: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 1130px; background-color: transparent; overflow: hidden; opacity: 0;"><iframe id="aswift_5" name="aswift_5" style="left: 0px; position: absolute; top: 0px; border: 0px; width: 1130px; height: 0px;" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="1130" height="0" frameborder="0" src="https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9051032955849697&output=html&h=280&adk=3038719801&adf=1743250800&pi=t.aa~a.4047818965~rp.1&w=1130&fwrn=4&fwrnh=100&lmt=1629105287&rafmt=1&to=qs&pwprc=5461822568&psa=1&format=1130x280&url=https%3A%2F%2Fwww.zhangshengrong.com%2Fp%2FYjNK4BPXW2%2F&flash=0&fwr=0&pra=3&rpe=1&resp_fmts=3&wgl=1&fa=40&dt=1629105280987&bpp=1&bdt=1364&idt=1&shv=r20210809&mjsv=m202108100101&ptt=9&saldr=aa&abxe=1&cookie=ID%3Ded90065e6e67172c-2247652deec80098%3AT%3D1621953625%3ART%3D1621953625%3AS%3DALNI_MZQh_3DjacCo60sRkH8eKPGQ_-rrg&prev_fmts=1130x200%2C1130x200%2C0x0%2C1130x280%2C1263x578%2C1130x280&nras=5&correlator=1377094913137&frm=20&pv=1&ga_vid=847116081.1629105280&ga_sid=1629105280&ga_hid=1242984005&ga_fc=0&u_tz=480&u_his=1&u_java=0&u_h=720&u_w=1280&u_ah=680&u_aw=1280&u_cd=24&u_nplug=3&u_nmime=4&adx=67&ady=2741&biw=1263&bih=578&scr_x=0&scr_y=438&eid=20211866%2C31062297&oid=3&pvsid=705412846630679&pem=129&ref=https%3A%2F%2Fwww.google.com.hk%2F&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C1280%2C0%2C1280%2C680%2C1280%2C578&vis=1&rsz=%7C%7Cs%7C&abl=NS&fu=128&bc=31&ifi=6&uci=a!6&btvi=4&fsb=1&xpc=AkHYGfRAWs&p=https%3A//www.zhangshengrong.com&dtd=6914" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" data-google-container-id="a!6" data-google-query-id="CMDEgJCatfICFVEmvAodzhYLqg" data-load-complete="true"></iframe></ins></ins></ins></div><header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/wradZMVKXB/" rel="bookmark">浅谈Python中带_的变量或函数命名</a> </h2> </header> <article class="clearfix"> <div class="summary"> Python 的代码风格由 PEP 8 描述.这个文档描述了 Python 编程风格的方方面面.在遵守这个文档的条件下,不同程序员编写的 Python 代码可以保持最大程度的相似风格.这样就易于阅读,易于在程序员之间交流. python中的标识符可以包含数字.字母和_,但必须以字母或者_开头,其中以_开头的命名一般具有特殊的意义. 前后均带有双下划线__的命名 一般用于特殊方法的命名,用来实现对象的一些行为或者功能,比如__new__()方法用来创建实例,__init__()方法用来初始化对象, </div> </article> </section> <section> <header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/4yNqQowbNA/" rel="bookmark">浅谈Python中的私有变量</a> </h2> </header> <article class="clearfix"> <div class="summary"> 私有变量表示方法 在变量前加上两个下划线的是私有变量. class Teacher(): def __init__(self,name,level): self.__name=name self.__level=level #获取老师的等级 def get_level(self): return self.__level #获取名字 def get_in_name(self): return self.__name 动态方法无法读取私有变量 即使是动态方法也无法读取私有变量,强行读取会报错. # </div> </article> </section> <section> <header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/24NjoQpMXB/" rel="bookmark">浅谈Python中re.match()和re.search()的使用及区别</a> </h2> </header> <article class="clearfix"> <div class="summary"> 1.re.match() re.match()的概念是从头匹配一个符合规则的字符串,从起始位置开始匹配,匹配成功返回一个对象,未匹配成功返回None. 包含的参数如下: pattern: 正则模型 string : 要匹配的字符串 falgs : 匹配模式 match() 方法一旦匹配成功,就是一个match object对象,而match object对象有以下方法: group() 返回被 RE 匹配的字符串 start() 返回匹配开始的位置 end() 返回匹配结束的位置 span()返 </div> </article> </section> <section> <header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/9Oab8Az5Xd/" rel="bookmark">浅谈Python中threading join和setDaemon用法及区别说明</a> </h2> </header> <article class="clearfix"> <div class="summary"> Python多线程编程时,经常会用到join()和setDaemon()方法,今天特地研究了一下两者的区别. 1.join ()方法:主线程A中,创建了子线程B,并且在主线程A中调用了B.join(),那么,主线程A会在调用的地方等待,直到子线程B完成操作后,才可以接着往下执行,那么在调用这个线程时可以使用被调用线程的join方法. 原型:join([timeout]) 里面的参数时可选的,代表线程运行的最大时间,即如果超过这个时间,不管这个此线程有没有执行完毕都会被回收,然后主线程或函数都会接 </div> </article> </section> <section style="height: auto !important;"> <div class="google-auto-placed" style="width: 100%; height: auto; clear: both; text-align: center;"><ins data-ad-format="auto" class="adsbygoogle adsbygoogle-noablate" data-ad-client="ca-pub-9051032955849697" data-adsbygoogle-status="done" style="display: block; margin: 10px auto 15px; background-color: transparent; height: 0px;" data-ad-status="unfilled"><ins id="aswift_6_expand" style="display: inline-table; border: none; height: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 1130px; background-color: transparent;" tabindex="0" title="Advertisement" aria-label="Advertisement"><ins id="aswift_6_anchor" style="display: block; border: none; height: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 1130px; background-color: transparent; overflow: hidden; opacity: 0;"><iframe id="aswift_6" name="aswift_6" style="left: 0px; position: absolute; top: 0px; border: 0px; width: 1130px; height: 0px;" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="1130" height="0" frameborder="0" src="https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9051032955849697&output=html&h=280&adk=3038719801&adf=1184868601&pi=t.aa~a.4047810408~rp.1&w=1130&fwrn=4&fwrnh=100&lmt=1629105288&rafmt=1&to=qs&pwprc=5461822568&psa=1&format=1130x280&url=https%3A%2F%2Fwww.zhangshengrong.com%2Fp%2FYjNK4BPXW2%2F&flash=0&fwr=0&pra=3&rpe=1&resp_fmts=3&wgl=1&fa=40&dt=1629105280990&bpp=1&bdt=1368&idt=0&shv=r20210809&mjsv=m202108100101&ptt=9&saldr=aa&abxe=1&cookie=ID%3Ded90065e6e67172c-2247652deec80098%3AT%3D1621953625%3ART%3D1621953625%3AS%3DALNI_MZQh_3DjacCo60sRkH8eKPGQ_-rrg&prev_fmts=1130x200%2C1130x200%2C0x0%2C1130x280%2C1263x578%2C1130x280%2C1130x280&nras=6&correlator=1377094913137&frm=20&pv=1&ga_vid=847116081.1629105280&ga_sid=1629105280&ga_hid=1242984005&ga_fc=0&u_tz=480&u_his=1&u_java=0&u_h=720&u_w=1280&u_ah=680&u_aw=1280&u_cd=24&u_nplug=3&u_nmime=4&adx=67&ady=3421&biw=1263&bih=578&scr_x=0&scr_y=1119&eid=20211866%2C31062297&oid=3&pvsid=705412846630679&pem=129&ref=https%3A%2F%2Fwww.google.com.hk%2F&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C1280%2C0%2C1280%2C680%2C1280%2C578&vis=1&rsz=%7C%7Cs%7C&abl=NS&fu=128&bc=31&ifi=7&uci=a!7&btvi=5&fsb=1&xpc=xCZoYH9OrF&p=https%3A//www.zhangshengrong.com&dtd=7874" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" data-google-container-id="a!7" data-google-query-id="CID2upCatfICFdIJXAodauML9Q" data-load-complete="true"></iframe></ins></ins></ins></div><header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/v710KvnyXM/" rel="bookmark">浅谈Python中文件夹和python package包的区别</a> </h2> </header> <article class="clearfix"> <div class="summary"> pycharm右键新建时会有目录(文件夹)和python package两个选项,这两个到底有什么不同呢 1.原来在python模块的每一个包中,都有一个__init__.py文件(这个文件定义了包的属性和方法)然后是一些模块文件和子目录,假如子目录中也有__init__.py那么它就是这个包的子包了. 当你将一个包作为模块导入(比如从 xml导入 dom)的时候,实际上导入了它的__init__.py 文件. 2.而目录跟包唯一不同的就是没有__init__.py 文件,一个包是一个带有特殊文 </div> </article> </section> <section> <header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/x7XRMnmdNz/" rel="bookmark">浅谈Python中的模块</a> </h2> </header> <article class="clearfix"> <div class="summary"> 模块 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多编程语言都采用这种组织代码的方式.在Python中,一个.py文件就称之为一个模块(Module). 使用模块有什么好处? 当一个模块编写完毕,就可以被其他地方引用.我们在编写程序的时候,也经常引用其他模块,包括Python内置的模块和来自第三方的模块. 模块还可以避免函数名和变量名冲突.相同名字的函数和变量完全可以分别存在不同的模块中.但是也要注意,尽量不要与内置函数名字冲突. 如果不 </div> </article> </section> <section> <header> <h2 class="title"> <a href="https://www.zhangshengrong.com/p/OQNzrpxBNR/" rel="bookmark">浅谈Python中的继承</a> </h2> </header> <article class="clearfix"> <div class="summary"> 继承 Python 中所有的类都是object类的子类,而object 继承自type 继承分为 接口继承和实现继承 接口继承:使用父类的接口名,子类重写这个方法.尽可能的继承接口类,在子类中实现方法,鼓励对接口类的多继承,这样遵循接口隔离原则,有利于归一化设计,不提倡对抽象类进行多继承 实现继承:子类不需要实现任何东西,直接使用父类接口和实现会增强代码的耦合性,不推荐使用. 一些细节 类继承最终要被实例化,我们多数时候使用的还是对象而不是类.因此我们还是来一点点看继- 继承的过程 承仅仅是一种 </div> </article> </section> <!-- /related -->
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。