英文:
Does unpacking enums at the global scope increase memory usage?
问题
我有一个包含500多行Enum
定义的模块,要明确一点,这不仅仅是一个包含500行枚举的模块,而是包含多个Enum
。请纠正我,通过解包Enum
,我只是创建一个引用吗?
# app/enums.py
import enum
class MyEnum(enum.Enum):
A = 1
B = 2
C = 3
A, B, C = MyEnum.A, MyEnum.B, MyEnum.C
英文:
I have a module that contains a little over of 500 lines of Enum
definitions, to be clear it's not just single 500 line enum, but several Enums
. Please correct me if I misunderstand, by unpacking the Enum
I am only creating a reference?
# app/enums.py
import enum
class MyEnum(enum.Enum):
A = 1
B = 2
C = 3
A, B, C = MyEnum.A, MyEnum.B, MyEnum.C
答案1
得分: 1
它不会使用比其他变量分配更多的内存。分配总是只创建引用,通常在内部只是指针;您需要执行显式复制操作来复制数据。
l = [[1], [2], [3]]
A, B, C = l # 嵌套列表的引用
X, Y, Z = (x.copy() for x in l) # 嵌套列表的副本
英文:
It doesn't use any more memory than any other variable assignments. Assignments always just create references, which are generally just pointers internally; you need to perform an explicit copy operation to duplicate the data.
l = [[1], [2], [3]]
A, B, C = l # references to the nested lists
X, Y, Z = (x.copy() for x in l) # copies of the nested lists
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论