Unpacking enums at the global scope increase memory usage?

huangapple go评论61阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月9日 07:49:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定