I have an object (numpy.ndarray
, for example), that should be passed as an argument to several functions. In C++ in such cases I declared such arguments as const
, in order to show, that they must not be changed.
How can I do the same in Python?
I have an object (numpy.ndarray
, for example), that should be passed as an argument to several functions. In C++ in such cases I declared such arguments as const
, in order to show, that they must not be changed.
How can I do the same in Python?
Syntactically, you can't. You have two choices:
pass a copy of your object to these functions - this way, you won't have to worry about it being corrupted
create a wrapper, that implements the required interface, wraps your object, and doesn't allow modifications
The second option is of course only possible if you know what interface is expected and if it's simple.
Well, there's a trick especially for numpy arrays:
array.flags.writable=False
© 2022 - 2024 — McMap. All rights reserved.
tuple
is the answer for it. – Primateship