What is the difference between objects and instances in python
I would put it this way: An “object” is an “instance” of a “class”,
People freely use instance and object interchangeable.
1 Like
I would say it is ambiguous and can be confusing:
- anything is an “object” (classes, instances, functions, …)
- so any class is an “object”
- and any instance is an “object”
- in OOP theory object might denote an instance, depending on the used terminology
and further more:
- python has an object class: Built-in Functions — Python 3.12.2 documentation
- a class inherits from this object (try: “class Test: pass; assert isinstance(Test, object)”)
- and an instance is also an object (try: “class Test: pass; t = Test(); assert isinstance(t, object)”)
- …
“object” and “instance” are synonymous, but focusing on different things.
- An
object
is a value, a piece of data with its associated type, methods, etc, etc. - An
instance
is one example of a particularclass
; for example,42
is an instance of the typeint
.
Every value in Python is an object, and every object has a type. This means that every value is an instance of something. Calling something an “instance” is emphasizing the relationship between the type and the value; calling it an “object” is emphasizing the encapsulation of data and functionality.
Every value is an instance, every value is an object. The terms are more-or-less interchangeable.
5 Likes