Is it possible something like lvalue of perl or setf of lisp in python?

我的未来我决定 提交于 2019-11-29 16:04:19

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

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]

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.

Logan

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!