What is the guidance around list()? #1300
-
In Python, I usually define empty lists using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Mojo currently lacks a data type equivalent to Python's That said, I think from dis import dis
def f():
l = []
def g():
l = list()
print('f:')
dis(f)
print('g:')
dis(g) Result:
Similar overhead won't be observed in Mojo (at least at runtime), as it's compiled and names are resolved statically. |
Beta Was this translation helpful? Give feedback.
Mojo currently lacks a data type equivalent to Python's
list
, which is likely due to ongoing work on traits. An alternative, such asDynamicVector
, exists, but it is not yet integrated with theListLiteral
mechanism. Therefore, relying on constructors (akin to Python'slist()
) remains necessary.That said, I think
list()
is an anti-pattern in Python. Becauselist
is really a dynamic variable that usually binds to thebuildins.list
, Python will generate an extraLOAD_GLOBAL
so the resultant byte code is slightly worse. I don't think it will greatly affect the actual performance of your Python code, it's just a good to know.dict
has some slight overhead for the same reason.