有人可以写一个简单的python旧样式类和新样式类的例子吗?

时间:2011-11-03 11:38:20

标签: python

虽然有几篇关于新课程而非旧课程的文章,但我无法看到旧/新风格的基本例子。

2 个答案:

答案 0 :(得分:2)

唯一的语法区别是新样式类继承自object:

class Old:
    ...

class New(object):
    ....

答案 1 :(得分:0)

我相信this wiki post正是您所寻找的:

 <snip> 
 The minor syntactic difference is that New Style Classes happen
 to inherit from object.

 class Old1:
     ...

 class Old2(Old1, UserDict): # Assuming UserDict is still old-style
     ...

 vs

 class New1(object):
     ... class New2(New1):
     ... class New3(Old1, New2):
     ... class New4(dict, Old1):  # dict is a newstyle class
 <snip>