Issue
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'm hoping someone could help to break down this vital concept for me, or kindly point me to more resources. Thanks!
Solution
To go deep, you need to understand the Python data model.
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.
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.
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.
Answered By - kojiro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.