What are Python's type "objects" exactly? - Stack Overflow
What exactly is an object in Python?
oop - What is an Object in Python? - Stack Overflow
Everything in Python is an object.
I'll answer the 1,2 question first, then 4th then 3rd:
- "Whats the relationship between a type type "objects" and "class instances" type objects?"
- "Can I assume the ~meta API to in-built type objects is the same as that of "class instance" type objects?"
They are the same, and yes they share a common API. When the documentation describes built in types as "objects", or class instances as "objects", or a class or whatever as an "object" ... they are talking about exactly the same language construct.
- "In general, what are "objects" ..."
The object is a foundational language feature in Python that supports attributes and behaviors much like other OOPLs. All Python objects also have a class much like other classed based OOPLs. The object class is the base of the class hierarchy in Python. Thus all classes are subclasses of the object class, and all the aforementioned "objects" and instances of object - by way of inheritance.
It's worth first pointing out explicitly that in Python (2.2 and above) "type" and "class" mean the same thing (for all intents and purposes). So "int", and the rest of the so called builtin types are classes (which are represented as objects of course). For example this x = int(1) calls the int class (object) to construct an int instance object, x.
It's true to say there are two types of object in Python; "type" objects, or those that represent types, and "non-type" objects - those that don't. But it's equally true to say there are two type of integers; zero, and not zero. It doesn't mean much: Everything in Python is an object including classes. Since classes form a kind object, they are all instances of a class called "type". The type object is also an instance of type. Note you can inspect the inheritance hierarchy of class by examining the _bases_ attribute of a class object. In all cases it leads back to the object class - of course. See https://www.eecg.utoronto.ca/~jzhu/csc326/readings/metaclass-class-instance.pdf for further details on this.
- "...where is it all documented?"
Well, that's actually a good question. It should be covered in the Data Model section of the language reference, but it is sort of skimmed over. The constructor for object objects, object (that made sense) is a built in and documented with the rest of the builtins here. Also the Classes chapter of The Python Tutorial also covers this area a bit.
It's a bit hard to understand what you are asking.
A type is the class of a class. Like everything else in Python, classes themselves are objects, and you can pass them around, assign them to variables, etc. If you ask a class what its class is, you will get the answer type. If you ask a class instance what its class is, you will of course get the class.
>>> type(int)
<type 'type'>
>>> type(1)
<type 'int'>
>>> class Foo(object):
... pass
>>> type(Foo)
<type 'type'>
>>> obj = Foo()
>>> type(obj)
<class '__main__.Foo'>
(here the function type(x) is another way of doing x.__class__.)
Thinkpython book by Allen Downey defines objects as something a variable can refer to to which he adds "object" and "value" can be used interchangeably. So, if my understanding serves me well, a variable stores a value, for e.g. x = 5, does that imply 5 is an object as well?
Then, I come across "file object" which is known to bear reference to a file. Maybe I'm overcomplicating things that's why I can't seem to grasp these concepts.
Everything is an object
An object is a fundamental building block of an object-oriented language. Integers, strings, floating point numbers, even arrays and dictionaries, are all objects. More specifically, any single integer or any single string is an object. The number 12 is an object, the string "hello, world" is an object, a list is an object that can hold other objects, and so on. You've been using objects all along and may not even realize it.
Objects have types
Every object has a type, and that type defines what you can do with the object. For example, the int type defines what happens when you add something to an int, what happens when you try to convert it to a string, and so on.
Conceptually, if not literally, another word for type is class. When you define a class, you are in essence defining your own type. Just like 12 is an instance of an integer, and "hello world" is an instance of a string, you can create your own custom type and then create instances of that type. Each instance is an object.
Classes are just custom types
Most programs that go beyond just printing a string on the display need to manage something more than just numbers and strings. For example, you might be writing a program that manipulates pictures, like photoshop. Or, perhaps you're creating a competitor to iTunes and need to manipulate songs and collections of songs. Or maybe you are writing a program to manage recipes.
A single picture, a single song, or a single recipe are each an object of a particular type. The only difference is, instead of your object being a type provided by the language (eg: integers, strings, etc), it is something you define yourself.
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.