pyimgui
pyimgui copied to clipboard
Why did we choose to make Vec2 and Vec4 namedtuples?
Hello! Using namedtuples make those vectors immutable, which makes them way less convenient to use.
vec2.x = 2.0
AttributeError: can't set attribute
It seems like there are mutable alternatives where you can set the components of the vector directly, were those too difficult to implement? Or maybe it was a conscious choice because of limitations (sounds weird since the C++ version does allow it)
https://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python
Cheers!
Maybe this doesn't address your problem, but "modifying" a namedtuple doesn't have to be too bad:
new_vec2 = vec2._replace(x=2.0)
In core.pyx
, Vec2
and Vec4
are mostly used as tuples of 2/4 values to represent width/height, window padding, item spacing, RGBA values, etc.
The naming is indeed a little unfortunate in a sense that it suggests they are vectors when they are not "proper vectors"; however tuple
or namedtuple
seem appropriate for their use case.
What task does the todo
tag refer to?
It is an old topic but I think the todo
tag was for testing how we could change this to be mutable. Indeed it makes sense in the context those are used but I can see why someone would want to construct its Vec2
or Vec4
in multiple steps or edit a component of its vector before sending it to a function. Especially since they could be accessible as editable attributes of structs like IO or as return values from some functions such as GetClipRectMin()
.
We need to cast whatever type they are defined in Python as a ImVec2 anyway before forwarding them.
Thank you!