python perl lisp,是否可能像python中的perl的lvalue或lisp的setf一样?
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:
>>> foo() = 3
File "", line 1
SyntaxError: can't assign to function call
There are however two special cases in the Python syntax:
# Slice assignment
a = [1,2,3,4]
a[0:2] = 98, 99 # (a will become [98, 99, 3, 4])
# Tuple assignment
(x, y, z) = (10, 20, 30)
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).
# Pass the function foo where to store the result
foo( lambda value : setattr(myObject, "member", value) )
總結(jié)
以上是生活随笔為你收集整理的python perl lisp,是否可能像python中的perl的lvalue或lisp的setf一样?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java语言怎么建立窗口awt,java
- 下一篇: datax oracle mysql_从