I understand the principal theory behind Everything is an Object but I really don't understand how it is implemented under the hood.
So: foo(4)
is the same as foo.__call__(4)
. But what is stopping me from doing foo.__call__.__call__(4)
?
foo
is a function and foo.__call__...
are all method wrappers around the function but when I call a function, which of these is even invoked?
My function of foo
has a lot of properties, and each of those objects store a lot of properties, so how does it not take up infinite memory?
sys.getsizeof('a')
produces 22
, which seems quite large for one character, but quite small as it references 71 properties.
I guess what I am asking is if I wanted to implement a naïve version of python (I don't but it seems the best way to ask) how would I implement this?
Edit 1
I had a bit of a look at builtins and realised that they are referencing the same properties (id('a'.upper) == id('b'.upper)
). Which makes me ask how it knows what object it is accessing?
Edit 2 As pts points out 'a'.upper is not 'b'.upper
, so that clears that up.
I've looked at the source for IronPython as I thought it would help me understand but it has confused me even more.
It means that everything from an int to a string to a function to a class is an object with methods and "constuctors" and the whole shebang.
Summary. In Python, everything is an object. Classes are objects, instances of classes are objects, modules are objects, and functions are objects. Anything that you can point a variable to is an object.
Everything Is an Object In object-oriented programming languages like Python, an object is an entity that contains data along with associated metadata and/or functionality. In Python everything is an object, which means every entity has some metadata (called attributes) and associated functionality (called methods).
- [Instructor] In object-oriented programming, everything is an object. In fact, we have been working with objects all this time. You might just not have realized it. Let's take a simple integer, int my value equals five.
I think what you're getting confused with is that although all of Python's variables might be objects and all the properties of those variables might be objects, there is a limit. I mean, for a normal class the structure usually goes:
myclass -> classobj -> type
which you can see if you try this in the console:
>> class A:
.. pass
..
>> print type(A)
<type 'classobj'>
>> print type(type(A))
<type 'type'>
but if you try and go deeper than type
, you just get type
. type
is the base object for all objects in Python.
>> print type(type(type(A)))
<type 'type'>
>> print type(type(type(type(type(A)))))
<type 'type'>
Not only do you get the same base class/object type, but you get the same instance of that type for the class instance of A, so although you can do an infinite recursion of the function type
(ie. type(type(type(type(... ))))
), you won't be going anywhere (ie. not exploring a bottomless pit of infinite memory.)
>> id(type(type(type(A)))
505578544
>> id(type(type(type(type(A))))
505578544
The same principle applies for all the objects in Python and all the properties of the objects in Python since they too are objects, but they are all finite.
Answering one of your earlier questions,
foo.__call__
is the same instance of 'method-wrapper' asfoo.__call__.__call__
So nothing is stopping you from doing a very long line with foo.__call__.__call__.__call__
...
Python plays a trick on you in that foo.__call__
gives a instance to 'method-wrapper', and inside that 'method-wrapper' class there is a function/variable also called __call__
that points to the same instance of that class.
Your assumption that if everything is an object, foo(4)
must automatically translate to foo.__call__(4)
is wrong.
Python can first check if the object foo
is a function, and if so call it, and do other things (like look for a __call__
attribute) otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With