OOP conceptual question: What does (object) means?

In python everything is an object. Everything is a class.

>>> type(1)
<class 'int'>
>>> type('1')
<class 'str'>
>>> type(type(1))
<class 'type'>
>>> type([])
<class 'list'>
>>> class MyClass(): pass
...
>>> type(MyClass())
<class '__main__.MyClass'>

An object just refers to a specific piece of something. That can be scrammed somewhere usually. So like a string, is an object, because its been created, and now can be manipulated.

Usually anyways, but for the class thing, you can easily see everything being a class because everything has specialized built in methods right?

>>> list = [2,34,45,542,21,65,6,4]
>>> list.sort()
>>> list
[2, 4, 6, 21, 34, 45, 65, 542]

Yet like class's it also has an instance, with its specific value and specialized methods, ect..

Every data type is similar to some extant, yet different, so you could refer to X list, has different then Y list, yet similar, compared to V string(yet i 'could' store values in strings like a list, good luck trying to scram class's in there!....). Either way, the point of calling something an object is just because its... something in 'specific'.

Inheriting from (object) is a bit like creating a string, or a list, you have specific base properties, compared to not having object(in python 2 anyways). But inheritance isn't usually thought of has an object in specific, where referring to the 'parent' 'object' class that's all, inheritance is usually like a blue print to make an object. So when you do [] to make a list, you are inheriting from base class 'list' and making a list with those properties. It wasn't an object 'before' 'that'.

This could differ in other languages, but don't get confused when someone says an object, they're usually talking about a piece of data. Depending on the situation this could be more or less specific...... programming is a really abstract thing so you'll need to get used to this.

But if you make the instance of a class, you call that an object(but not the class it self), similarly you call a string an object, or a list an object, but you don't call it an object before it exists, because you din't create it yet. When you do:

class MyClass(object):

    def __init__(self, value):
        self.value = value

    def return_value(self):
        return self.value

C = MyClass('Reddit')
R = MyClass('Try later!!')
print(C.return_value())

Output:

Reddit

You are making a class, but the instance of that class is 'C' because you entered values because init needed 1 value, it then set it to self, making it 'global' within the instance of the class, then when you asked for it with the return method, it gave it to print, which outputs it to the terminal.

Yet we din't print R because R is in a different scope/instance, then C. Play around with this a lot, it takes a while to understand. Inheritance is the slightly easy useless part to fully understand, usually you want to avoid it unless it happens naturally.

/r/learnpython Thread