In lisp you can say:
(setf (aref a 1) 5)
In perl you can say:
substr( $string, $start, $stop ) =~ s/a/b/g
Is it possible something like this in python? I mean is it possible to use function result as a lvalue (as a target for assignment operation)?
No. Assigning to the result of a function call is specifically prohibited at the compiler level:
There are however two special cases in the Python syntax:
Note also that in Python there is a statement/function duality and an assignment or an augmented assignment (
+=
,*=
...) is not just a normal operator, but a statement and has special rules.Moreover in Python there is no general concept of "pointer"... the only way to pass to a function a place where to store something is to pass a "setter" closure because to find an assignable place you need to use explicit names, indexing or you need to work with the instance dictionary if the place is an object instance member).
No, there isn't any way to do this in general. The slice notation comes close in a limited case, as you can do things like this:
In short, no.
However, if you define
__setitem__
, you can assign to a subscript, e.g.And you could return
foo
(and also the subscript, if you wanted) from a function.or, in one line:
See
operator
.Generally, no (don't stop reading!!!!). Observe the following:
However, doing some searching I found this (which looks like bad practice, but usable):
So, mixing your Perl with my Python we get:
To mitigate massive downvoting, I should mention you can totally screw up your program with this. But you probably know that. Just make sure you don't overwrite existing variables when using this method by checking
"somevar" not in locals()
or"somevar" not in globals()
.