python中什么是新式类

  介绍

这篇文章给大家介绍python中什么是新式类,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

python主要用来做什么

python主要应用于:1,网络开发;2、数据科学研究;3,网络爬虫;4、嵌入式应用开发,5日游戏开发;6桌面应用开发。

<强> 1,说明

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

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

<强> 2,实例

 #, newstyle.py, python环境为2. xclass 经典:
  ,,,,,,
  ,,,python2.x默认类为经典类
  ,,,由于__getatt__ 与,__getattribute__功能效果一样,这里只用__getattr__演示
  ,,,,,,
  ,,,def  __getattr__(自我,,method_name):
  ,,,,,,,印刷(“call  Classic  __getattr__ it  would  call 内置(% s), method “, %, method_name),,,,,,,,,,,,,,,, return  getattr (self.__name, method_name) class  NewStyleClass(对象):,,,,def  __init__(自我):
  ,,,,,,,self.__name =,“newstyle  name"
  ,,,,,,
  ,,,python2.x需要指明为新式类,python3.x默认为新式类
  ,,,,,,
  ,,,def  __getattr__(自我,,项):
  ,,,,,,,印刷(“call  NewStyle  __getattr__ it  would  call 内置(% s), method “, %项),,,,,,,,,,,,,,,,return  getattr (self.__name、项目)def  test_dir ():
  ,,,C =,经典的()
  ,,,N =, NewStyleClass ()
  ,,,print (dir (C),,,,,,,, #,经典类内置有__getattr__方法
  ,,,print (dir (N),,,,,,,, #,新式类的内置方法继承对象对象祝辞祝辞祝辞,python 纽斯特尔。py 

关于python中什么是新式类就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。

python中什么是新式类