简体   繁体   English

python如何在内存中存储对象

[英]How does python store objects in memory

It is my understanding that a list is a block of contiguous memory locations that contain pointers to the element values in memory. 据我了解,列表是一块连续的内存位置,其中包含指向内存中元素值的指针。 As shown below: 如下所示:

内存中列出

My question is whether its the same with objects in memory ie: suppose I have a point class with an init method implemented as follows: 我的问题是它与内存中的对象是否相同,即:假设我有一个带有以下实现的init方法的点类:

class Point: 
    def __init__(self, x, y): 
        self.x = x
        self.y = y

And I create a new point object: 然后创建一个新的点对象:

p = Point(4,6)

Would the pointer to px and the pointer to py be next to each other in memory? 指向px的指针和指向py的指针在内存中会相邻吗?

Python - that is, the programming language - has no concept of memory locations or pointers. Python(即编程语言)没有内存位置或指针的概念。 So from that point of view, there is no answer to your question. 因此,从这个角度来看,您的问题没有答案。 Your question can only be answered for a specific implementation of python, like CPython . 您的问题只能针对python的特定实现 (例如CPython)回答。

That said, the answer is going to be no for pretty much every implementation of python. 就是说,对于几乎所有的python实现,答案都是“ ”。 That's because (most) python classes store their attributes in a dictionary. 这是因为(大多数)python类将其属性存储在字典中。 We can access this dictionary through the name __dict__ : 我们可以通过名称__dict__访问此字典:

>>> Point(1, 2).__dict__
{'x': 1, 'y': 2}

Since dicts have to be efficient and have an O(1) lookup time, they are usually implemented as hash tables . 由于字典必须高效并且具有O(1)查找时间,因此通常将其实现为哈希表 The chance of x and y being right next to each other in a hash table is very slim. xy在哈希表中彼此紧邻的机会非常小。

Not all classes use a dictionary to store attributes, though. 但是,并非所有类都使用字典来存储属性。 There are two exceptions: 有两个例外:

  • Built-in classes like dictionaries, integers, strings, etc. 内置类,如字典,整数,字符串等。
  • Classes with a __slots__ attribute 具有__slots__属性的类

The special __slots__ class variable allows us to tell python which attributes the class will have. 特殊的__slots__类变量允许我们告诉python该类将具有哪些属性。 Python will then only allocate just enough memory to store these attributes, and trying to create any other attributes will cause an error: 然后,Python将仅分配足够的内存来存储这些属性,并且尝试创建任何其他属性将导致错误:

class Point: 
    __slots__ = ('x', 'y')

    def __init__(self, x, y): 
        self.x = x
        self.y = y
>>> p = Point(1, 2)
>>> p.x
1
>>> p.__dict__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Point' object has no attribute '__dict__'
>>> p.z = 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Point' object has no attribute 'z'

If a class uses __slots__ , it's far more likely that all of its attributes are stored next to each other in memory. 如果一个类使用__slots__ ,则其所有属性很可能彼此相邻地存储在内存中。 But it's still no guarantee. 但这仍然不能保证。 The python implementation can store the attributes however it wants. python实现可以根据需要存储属性。

Not in general because the attributes of an ordinary object are stored in a hash table, keyed by the attribute name. 通常不会这样,因为普通对象的属性存储在哈希表中,并以属性名称作为关键字。 Other implementations are possible (as in Self and some Javascript implementations), but to my knowledge, no Python implementation currently does this. 其他实现也可以(例如Self和某些Javascript实现),但是据我所知,目前还没有Python实现。

If you used tuples, as in collections.namedtuple (the implementation is quite instructive), then the pointers would be adjacent. 如果使用了元组(如collections.namedtuple (实现非常有启发性),则指针将是相邻的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM