开发者

A basic question about python class definitions [duplicate]

This question already has answers here: 开发者_JAVA百科 Why do Python classes inherit object? (6 answers) Closed 5 years ago.

What is the difference between saying:

class foo:
    pass

and

class foo(object):
    pass

?


The latter declares a new-style class.


"Classes and instances come in two flavors: old-style (or classic) and new-style." http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes


Old-style classes don't extend the built-in 'object' type. New-style classes do. If you're writing new code, use new-style classes. If you're working with old code that is already using old-style classes, well... I say go with it just to insure that all classes act the same way.

Here's a good rundown of the differences: http://docs.python.org/release/2.5.2/ref/node33.html And here's a collection of links for more: http://www.python.org/doc/newstyle/

I'll note that new-style classes were introduced in Python 2.2, so if you're seeing code that uses old-style classes, and you're going to use it with, say, Python 2.7, you might want to run a quick test to make sure it's not so aged that it doesn't work in newer versions of Python.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜