When is del useful in python?

前端 未结 21 2231
野趣味
野趣味 2020-11-22 11:58

I can\'t really think of any reason why python needs the del keyword (and most languages seem to not have a similar keyword). For instance, rather than deletin

21条回答
  •  没有蜡笔的小新
    2020-11-22 12:16

    One place I've found del useful is cleaning up extraneous variables in for loops:

    for x in some_list:
      do(x)
    del x
    

    Now you can be sure that x will be undefined if you use it outside the for loop.

提交回复
热议问题