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

后端 未结 4 911
孤独总比滥情好
孤独总比滥情好 2020-12-21 12:03

In lisp you can say:

(setf (aref a 1) 5)

In perl you can say:

substr( $string, $start, $stop ) =~ s/a/b/g

4条回答
  •  没有蜡笔的小新
    2020-12-21 12:33

    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.

提交回复
热议问题