What is the difference between objects and classes

2020-01-28 09:20发布

问题:

I am a self-taught python user (kind of.). I read much to deepen my knowledge about python. Today

I encountered a text saying:

... classes and objects ....

So I was wondering what is the difference between objects and classes in python. I taught all classes are objects, but in that case, author wouldn't have used phrase "classes and objects". I'm confused...

回答1:

These are two closely related terms in object oriented programming. The standard meaning is that an object is an instance of a class.



回答2:

An object is an instantiation of a class.

Think of a class like the blueprint of a car.

Ford make cars (objects) based on the rules and information enclosed in the blueprint.



回答3:

Yes, classes (and functions, and modules, and basically everything) in Python are objects, too. The difference lies in their types:

class Foo(object): pass
print type(Foo)
print type(Foo())

To see they're both objects, you can check that they both have attributes:

print dir(Foo)
print dir(Foo())


回答4:

A class is an idea. An object is the fruition of that idea.



回答5:

A class describes what that object will be, but it isn't the object itself.