首页IT科技python3.6新特性(python新式类是什么)

python3.6新特性(python新式类是什么)

时间2025-09-18 10:42:56分类IT科技浏览5380
导读:1、说明...

1                、说明

python3.x的所有类都会自动转换为一个新式类                ,不论是否有继承object对象                。

python2.x必须显式地指定类继承object父类才表示新式类                        。

2                         、实例

#newstyle.py,python环境为2.xclassClassic: """ python2.x默认类为经典类 由于__getatt__与__getattribute__功能效果一样                         ,这里只用__getattr__演示 """ def__getattr__(self,method_name): print("callClassic__getattr__,itwouldcallbuilt-in[%s]method"%method_name)returngetattr(self.__name,method_name)classNewStyleClass(object):def__init__(self): self.__name="newstylename" """ python2.x需要指明为新式类        ,python3.x默认为新式类 """ def__getattr__(self,item): print("callNewStyle__getattr__,itwouldcallbuilt-in[%s]method"%item)returngetattr(self.__name,item)deftest_dir(): C=Classic() N=NewStyleClass() print(dir(C)#经典类内置有__getattr__方法 print(dir(N)#新式类的内置方法继承object对象>>>pythonnewstyle.py

以上就是python新式类的介绍            ,希望对大家有所帮助         。更多Python学习指路:Python基础教程

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
火车头采集器插件读取代理IP(优化你的WordPress火车头采集发布文章时间,确保准确无误!) win7管理员账号锁定(Win7系统管理员Administrator账户不见了怎么办?)