Is it possible something like lvalue of perl or se

2019-01-28 19:40发布

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

4条回答
虎瘦雄心在
2楼-- · 2019-01-28 20:14

No. Assigning to the result of a function call is specifically prohibited at the compiler level:

>>> foo() = 3
  File "<stdin>", 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) )
查看更多
你好瞎i
3楼-- · 2019-01-28 20:17

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:

>>> a = [1, 2, 3]
>>> a[1:2] = [5, 6]
>>> a
[1, 5, 6, 3]
查看更多
爷的心禁止访问
4楼-- · 2019-01-28 20:17

In short, no.

However, if you define __setitem__, you can assign to a subscript, e.g.

foo['subscript'] = 7

And you could return foo (and also the subscript, if you wanted) from a function.

container, subscript = whatevs()
container[subscript] = 7

or, in one line:

operator.setitem(*(whatevs()+(7,)))

See operator.

查看更多
倾城 Initia
5楼-- · 2019-01-28 20:25

Generally, no (don't stop reading!!!!). Observe the following:

class test:
    test = 4

test().test = 5
# we can no longer refer to the created object.
x = test().test = 6
x # 6

However, doing some searching I found this (which looks like bad practice, but usable):

globals()["varname"] = 5
varname # 5

So, mixing your Perl with my Python we get:

globals()[substr( $string, $start, $stop )]  = something
substr( $string, $start, $stop ) # something

# Note: wouldn't work because the function returns a string. 
# I just don't know what the function returns. 
# so exec("print " +substr( $string, $start, $stop ) I guess

# similarly, and possibly a little better practice
locals()["somethingdif"] = somethingelse
somethingdif # somethingelse

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

查看更多
登录 后发表回答