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) )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值