旧式和新式的Python 2.7? [英] old-style and new-style classes in Python 2.7?

查看:137
本文介绍了旧式和新式的Python 2.7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


Python 2.7中的新式和旧式类的当前状态是什么?我不经常使用Python,但我模糊地记住了这个问题。文档似乎没有提到这个问题: Python教程:类。我还需要担心这个吗?一般来说,我应该声明我的类如下:

  class MyClass:
pass



或?

  class MyClass ):
pass


解决方案

。这些是新式样的类。




  • 你已经准备好了python 3。


  • .super()可以正常运作,



Possible Duplicate:
Old style and new style classes in Python

What is the current state of affairs with new-style and old-style classes in Python 2.7? I don't work with Python often but I vaguely remember the issue. The documentation doesn't seem to mention the issue at all: The Python Tutorial: Classes. Do I still need to worry about this? In general should I declare my classes like:

class MyClass:
    pass

or?

class MyClass(object):
    pass

解决方案

Always subclass "object". Those are new style classes.

  • You are ready for python 3 that way.

  • Things like .super() work properly that way, should you need them.

这篇关于旧式和新式的Python 2.7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆