What is the difference between objects and classes

2020-01-28 08:41发布

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...

5条回答
Evening l夕情丶
2楼-- · 2020-01-28 09:24

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

查看更多
老娘就宠你
3楼-- · 2020-01-28 09:36

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.

查看更多
地球回转人心会变
4楼-- · 2020-01-28 09:43

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

查看更多
女痞
5楼-- · 2020-01-28 09:43

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())
查看更多
Anthone
6楼-- · 2020-01-28 09:44

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

查看更多
登录 后发表回答