is there any way to prevent side effects in python?

后端 未结 7 2156
别跟我提以往
别跟我提以往 2021-02-05 15:14

Is there any way to prevent side effects in python? For example, the following function has a side effect, is there any keyword or any other way to have the python complain abou

7条回答
  •  天命终不由人
    2021-02-05 16:03

    No, but with you example, you could use immutable types, and pass tuple as an a argument. Side effects can not affect immutable types, for example you can not append to tuple, you could only create other tuple by extending given.

    UPD: But still, your function could change objects which is referenced by your immutable object (as it was pointed out in comments), write to files and do some other IO.

提交回复
热议问题