I am very confused about what is object and what can behave as an object, see the example I created to illustrate the situation:
def subtrai(x,y):
return (x - y)
class OpeMatematica(object):
def __init__(self, x, y):
self.x = x
self.y = y
def soma(self):
return (self.x + self.y)
soma = OpeMatematica(10,10)
print(soma.soma())
print(subtrai(100, 10))
s = subtrai
print(s(50,10))
s = soma
s.x = 10
s.y = 100
print(s.soma())
I created a subtrai
function that performs a subtraction and created a OpeMatematica
class with the attributes x
and y
and a soma
method.
In this line:
soma = OpeMatematica(10,10)
print(soma.soma())
I instantiated my class OpeMatematica
and invoked method soma
and got the result 20
.
In this line:
print(subtrai(100, 10))
Invoke the function subtrai
and get the result 90
.
In this line I discovered something interesting:
s = subtrai
print(s(50,10))
See that I assign the variable s
to the function subtrai
and it has passed the behavior of the function subtrai
see s(50,10)
, it is as if the subtrai
function was an object and could be instantiated, the result of the operation is 40
.
And in this line has another curiosity:
s = soma
s.x = 10
s.y = 100
print(s.soma())
Assigns the s
to the object soma
and s
to an object of type OpeMatematica
, accessing the attributes x
and y
and invoking the soma
method, the result is 110
.
Now I'm confused as to what a Python object is, analyzing this code I concluded that everything seems to be a Python object, even variables and functions , someone could you explain to me why Python has this behavior, and what is an object or a class in Python?