Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const arguments 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?

like image 702
Felix Avatar asked Oct 20 '22 09:10

Felix


1 Answers

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.

like image 171
BartoszKP Avatar answered Oct 23 '22 04:10

BartoszKP