The pair of concepts that you are looking for are mutable/immutable parameters and in-place/returning of results.
In your examples:
You have to set the string to "string dot replace", because the replace function operates on a string which, in python, is immutable so the replace function returns a new string.
For a C/C++ programmer this is more familiar as parameters "passed by value", rather than "passed by reference", which makes them immutable and returning the result.
You don't need to set an array equal to "array dot reverse", because reverse operates on an array, which is mutable, so is able to make changes in-place before returning.
In languages such as C/C++ this is known as parameters "passed by reference" i.e. passing the address which, if unmodified by const
, allows the function to change, mutate, the contents of that address altering the results in-place before returning.
Of course it is not unusual to have a function that returns results by both mechanisms, e.g. int SomeFn(int p1, int p2, int *ErrCode)
can, potentially return results both in the return value and by modifying the contents of ErrCode
.
A 3rd Method
For completeness a 3rd mechanism for returning results is by side-effect or global, i.e. modifying file scope, program wide, shared or environmental values. This is generally considered bad news as, unless very well documented, you can only find out what is being changed by careful reading of the code. In languages such as C/C++ this is all too easy to do by having an outer scope variable with a given name, possibly even in another module, and no masking local scope variable of the same name. In Python, while you can read the values of values in outer scopes, unless outer scope values are explicitly set as available to be modified with the global
keyword, attempting to modify an outer scope variable automatically creates a local of the same name.
Best Answer
Usually this is called a type signature.