13

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!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
jchua
  • 354
  • 1
  • 4
  • 15
  • 3
    This question is too broad. The answer could range from a single paragraph to a blog post. Please do some reading up, especially from the documentation. In a nutshell, `object` is the most basic type in python; everything you'll ever interact with is some kind of object that has more specific properties and behaviour. – cs95 May 26 '19 at 02:13
  • 2
    I agree the question is broad: The canonical answer is the [Python data model](https://docs.python.org/3/reference/datamodel.html). I'd like to hear from O.P. if they can narrow down what they're asking, though. – kojiro May 26 '19 at 02:15
  • 2
    Hi guys I really appreciate your comments, I understand now, how it was vague. Unfortunately, my vocab in programming is still really elementary, hence was not able to express myself on a level you guys commonly communicate in. I will accept kojiro's answer below, as it was actually what I am looking for. I am concluding that "Objects" are basic but yet can go extremely deep, and will be more relaxed on finding a definite answer for now. Thanks all! – jchua May 26 '19 at 02:50

8 Answers8

19

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    Hi Bryan, this helps alot too, appreciate your answer! – jchua May 26 '19 at 08:23
  • Thank you. Are there examples of what is not an object in Python? (maybe in Python 2 there were some non-objects?) – Yaroslav Nikitenko Feb 27 '20 at 12:37
  • https://stackoverflow.com/questions/865911/is-everything-an-object-in-python-like-ruby Everything is an object – Rotkiv Jan 26 '21 at 05:06
  • "Everything" is unnecessarily vague. A `for` loop is not an object. All *values* are objects. A `def` statement is not an object, but the function it defines (an object of type `function`, for example) is. – chepner Nov 23 '22 at 14:21
9

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.

kojiro
  • 74,557
  • 19
  • 143
  • 201
  • 1
    Hi kojiro, this helps point in the direction whereby I can read up more on (especially on Python's data mode) - was already reading about this in Mark Lutz's book, however, was not able to draw the connections. However, your answer has helped confirm my direction. Thanks much. – jchua May 26 '19 at 02:58
  • Thank you. From the link I can see that a function, an object method, a class and a module are all objects. Do you know examples of what is not an object? (`type` seems to be also an object, maybe something has changed from Python 2?) – Yaroslav Nikitenko Feb 27 '20 at 12:36
  • There isn't really anything reference-able in Python that is not an object. I don't really think this has changed substantially since Python 2. – kojiro Feb 27 '20 at 12:50
4

I think that the O.P. has a very good question. Object is a word that should come with a definition. I found this one that made things simpler for me. Maybe it will help you have a general concept of what an object is.

Objects: Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes. Classes are essentially a template to create your objects.

See: https://www.learnpython.org/en/Classes_and_Objects for further information.

This might not be a complete definition but it is a concept I can understand and work with.

3

In the context of Python and all other Object Oriented Programming (OOP) languages, objects have two main characteristics: state and behavior.

You can think of a constructor as a factory that creates an instance of an object with state and behavior.

State - Any instance or class variables associated to that object.

Behavior - Any instance or class methods

The following is an example of a class, in Python, to illustrate some of these concepts.

class Dog:
    SOUND = 'woof'
    def __init__(self, name):
        """Creates a new instance of the Dog class.

        This is the constructor in Python.
        The underscores are pronounced dunder so this function is called
        dunder init.
        """ 
        # this is an instance variable.
        # every time you instantiate an object (call the constructor)
        # you must provide a name for the dog
        self._name = name

    def name(self):
        """Gets the name of the dog."""
        return self._name

    @classmethod
    def bork(cls):
        """Makes the noise Dogs do.

        Look past the @classmethod as this is a more advanced feature of Python.
        Just know that this is how you would create a class method in Python.
        This is a little hairy.
        """
        print(cls.SOUND)

Though I agree with the comments that this question is a little vague. Please be a little more specific but the answer I've provided is a quick overview of classes and objects in Python.

Skam
  • 7,298
  • 4
  • 22
  • 31
  • `self.SOUND` would create an *instance* variable. – gmds May 26 '19 at 02:24
  • Heh, I'm upvoting this for covering _state_ and _behavior_ and in particular _instances_ of objects, which are the most elementary examples, and I'm slightly annoyed at myself for not covering that in my answer. – kojiro May 26 '19 at 02:29
  • Thanks Skam, appreciate this answer too! Sorry was not able to define my question more appropriately. – jchua May 26 '19 at 02:52
  • 1
    \_\_init\_\_ is not the constructor. You can't pass self into a constructor, since the constructor creates the object that self refers to. The instance is created by \_\_new\_\_, and \_\_init\_\_ initializes the attributes. – rdelmar Apr 01 '21 at 15:13
1

Your question was perfectly fine, nothing to apologize for. Some people are just arrogant.

Your question being broad is the only way to interpret that you're actually asking what the basic definition of "object" is; and in this case, for Python, the answer is "everything" < and that's not confusing at all.

Every building block is an object (any variable, function, tuple, string, etc). It's literally like having a Lego Set to build a Star Wars big ship and asking "hey, the manual says to be careful not break any piece; so, what is a piece?" < the answer would be; everything, every single individual element of that Lego Set is a piece.

Legit question.

Best,

Kagio
  • 11
  • 1
0

In OOP, object is a very simple yet complicated concept that people don't understands at first. I also had issue understanding it first but once you understands clearly what class is then it's so easy to understands what object is. If i am an object then my class is "Homo Sapiens". Let us know more about classes.

Class and Object:

Class is a blueprint that we create to have any object with some specific characteristics. Let us take an example of any fruit, Apple. Apple will fall in to the class named Fruits and it will have some attributes which would be the taste, smell, shape, etc. So we can say that Apple is an object of the class Fruits which has different attributes associated with it.

Now, let us take a technical example. If there is a class defined by any programmer and if he assigns any object to that class then it means that the object will have all the attributes and methods which that particular class consists.

For further knowledge of class and object, you can use the official python link which i am providing here.

https://docs.python.org/3/tutorial/classes.html

Hope i am little helpful to you.

Have a great day.

Mitul
  • 31
  • 3
0

The documentation says below:

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer”, code is also represented by objects.)

Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is’ operator compares the identity of two objects; the id() function returns an integer representing its identity.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
-2
  • An object is simply a collection of data (variables) and methods (functions) that act on data.

  • A class is a blueprint for that object.

Bhoomi Vaishnani
  • 718
  • 4
  • 19