我很惊讶之前没有人问过我的问题(措辞如上述).我希望有人可以在像 Python 这样的 OOP 语言的上下文中分解这个基本术语对象".以像我这样的初学者能够理解的方式进行解释.
I am surprised that my question was not asked (worded like the above) before. I am hoping that someone could break down this basic term "object" in the context of a OOP language like Python. Explained in a way in which a beginner like myself would be able to grasp.
当我在谷歌上输入我的问题时,出现的第一篇文章是此处.
When I typed my question on Google, the first post that appears is found here.
定义如下:对象是使用类的构造函数创建的.这个对象将被称为类的实例.
This is the definition: An object is created using the constructor of the class. This object will then be called the instance of the class.
维基百科是这样定义的:一个对象是一个类的实例.对象是一种抽象.它们既保存数据,又保存数据的方法.数据通常在对象外部不可见.
Wikipedia defines it like this: An object is an instance of a Class. Objects are an abstraction. They hold both data, and ways to manipulate the data. The data is usually not visible outside the object.
我希望有人能帮助我分解这个重要的概念,或者请指点我更多的资源.谢谢!
I am hoping someone could help to break down this vital concept for me, or kindly point me to more resources. Thanks!
推荐答案要深入,需要了解 Python 数据模型.
但是如果你想要一个有光泽的 stackoverflow 备忘单,让我们从字典开始.(为了避免循环定义,让我们同意至少,字典是键到值的映射.在这种情况下,我们甚至可以说键肯定是字符串.)>
But if you want a glossy stackoverflow cheat sheet, let's start with a dictionary. (In order to avoid circular definitions, let's just agree that at a minimum, a dictionary is a mapping of keys to values. In this case, we can even say the keys are definitely strings.)
def some_method(): return 'hello world' some_dictionary = { "a_data_key": "a value", "a_method_key": some_method, }一个对象就是这样一个映射,带有一些额外的语法糖,允许您使用点表示法访问键".
An object, then, is such a mapping, with some additional syntactic sugar that allows you to access the "keys" using dot notation.
现在,还有很多事情要做.(事实上,如果你想在 python 之外理解这一点,我推荐元对象协议的艺术.)您必须跟进但是什么是实例?"和你怎么能像这样对字典中的条目进行迭代?"和什么是类型系统"?Skam 的精彩回答中解决了其中一些问题.
Now, there's a lot more to it than that. (In fact, if you want to understand this beyond python, I recommend The Art of the Metaobject Protocol.) You have to follow up with "but what is an instance?" and "how can you do things like iterate on entries in a dictionary like that?" and "what's a type system"? Some of this is addressed in Skam's fine answer.
我们可以讨论 python dunder 方法,以及它们如何基本上是一种实现本机行为的协议,例如 sized(具有长度的事物)、可比类型(x < y)、可迭代类型等
We can talk about the python dunder methods, and how they are basically a protocol to implementing native behaviors like sized (things with length), comparable types (x < y), iterable types, etc.
但由于这个问题基本上是博士级别的广泛,我想我会非常简化我的答案,看看你是否想限制这个问题.
But since the question is basically PhD-level broad, I think I'll leave my answer terribly reductive and see if you want to constrain the question.
更多推荐
Python 中的对象是什么?
发布评论