Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
986 views
in Technique[技术] by (71.8m points)

oop - Difference between object and instance in python?

This happens in python2.7

I am working on the idea of meta class in python, almost all the tutorial refer object as instance of a class, in python. However, when playing with the class A(): form of defining a class, I saw this:

class ClsDef1():
    pass
C1 = ClsDef1()
print C1
<__main__.ClsDef1 instance at 0x2aea518>

class ClsDef2(object):
    pass
C2 = ClsDef2()
print C2
<__main__.ClsDef2 object at 0x2ae68d0>

This means when create a instance from a class that is not inherent from object, the instance is an instance, but when a class is inherent from object, the instance of the class is an object?

Could anyone explain the difference? In real life which one should I use?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the difference between new-style and old-style classes, which is explained in great detail in the documentation. Basically, in Python 2.x you should ensure you always inherit from object so that you get a new-style class. In Python 3, old-style classes have gone away completely.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...