一个对象的所有事物如何工作? [英] How does everything is an object even work?

查看:60
本文介绍了一个对象的所有事物如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解一切都是对象背后的原理,但是我真的不明白它是如何实现的.

I understand the principal theory behind Everything is an Object but I really don't understand how it is implemented under the hood.

所以:foo(4)foo.__call__(4)相同.但是什么阻止了我执行foo.__call__.__call__(4)?

So: foo(4) is the same as foo.__call__(4). But what is stopping me from doing foo.__call__.__call__(4)?

foo是一个函数,而foo.__call__...都是该函数的方法包装,但是当我调用一个函数时,哪一个甚至被调用?

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?

我的foo函数具有许多属性,并且每个对象都存储许多属性,那么它如何不占用无限内存呢?

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')生成22,对于一个字符来说似乎很大,但是由于引用了71个属性,因此很小.

sys.getsizeof('a') produces 22, which seems quite large for one character, but quite small as it references 71 properties.

我想我要问的是我是否想实现朴素的python版本(我没有,但这似乎是最好的询问方式),我将如何实现呢?

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?

编辑1 我对内置函数进行了一些了解,并意识到它们引用的是相同的属性(id('a'.upper) == id('b'.upper)).这使我问到它如何知道它正在访问哪个对象?

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?

编辑2 ,因为要指出的点 'a'.upper is not 'b'.upper,所以可以清除它.

Edit 2 As pts points out 'a'.upper is not 'b'.upper, so that clears that up.

我查看了IronPython的来源我以为它可以帮助我理解,但更让我感到困惑.

I've looked at the source for IronPython as I thought it would help me understand but it has confused me even more.

推荐答案

我想让您感到困惑的是,尽管Python的所有变量可能都是对象,而这些变量的所有属性都可能是对象,但是有一个限制.我的意思是,对于普通班级来说,结构通常是这样的:

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'>

,但是如果您尝试比type更深入的话,您只会得到type. type是Python中所有对象的基础对象.

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'>

您不仅会获得相同的基类/对象类型,而且会为A的类实例获得相同类型的实例,因此尽管您可以对函数type进行无限递归(即type(type(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

相同的原理适用于Python中的所有对象以及Python中对象的所有属性,因为它们也是对象,但它们都是有限的.

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.

回答您先前的问题之一

foo.__call__与'c16>

所以没有什么可以阻止您使用foo.__call__.__call__.__call__ ...

So nothing is stopping you from doing a very long line with foo.__call__.__call__.__call__...

Python在您身上扮演了一个诡计,因为foo.__call__为"method-wrapper"提供了一个实例,并且在"method-wrapper"类中有一个函数/变量(也称为__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.

这篇关于一个对象的所有事物如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆