Skip to content

对象

一句话解释

对象 是 特定 实例 的 类, 包含 它的 own 数据 (attributes) and behavior (方法).

用大白话说

object 是一种concrete instance created from a class。 While the class is the blueprint, the object is the actual thing built from it, with its own specific data.

In Python, everything is an object — integers, strings, lists, functions, and even classes themselves are objects. You can check an object's type with 类型() and its available attributes and methods with dir().

Each object has its own identity (memory address, checkable with 是), type, and value. Two objects can have the same value but different identities. Objects are created by calling the class like a function: obj = MyClass(args).

示例

python
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

p1 = Point(3, 4)
p2 = Point(3, 4)

print(p1.x, p1.y)    # 3 4
print(p1 is p2)      # False (different objects)
print(type(p1))      # <class '__main__.Point'>

# Everything is an object
print(type(42))      # <class 'int'>
print(type("hi"))    # <class 'str'>

对象 是 实例 的 类 使用 它们的 own 数据. In Python, 一切 是 对象.

常见混淆

  • 容易混淆的对象: 类

    关键区别: 类 是 定义/blueprint. 对象 是 特定 实例 创建 从 类. One 类 能 produce 许多 对象, 每个 使用 不同 attribute 值.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表