Pass list to function by value

后端 未结 4 1679
南旧
南旧 2020-12-06 18:24

I want to pass a list into function by value. By default, lists and other complex objects passed to function by reference. Here is some desision:

def add_at_         


        
4条回答
  •  北海茫月
    2020-12-06 18:53

    In case of ad is list you can simple call your function as add_at_rank(ad + [], rank).

    This will create NEW instance of list every time you call function, that value equivalented of ad.

    >>>ad == ad + []
    True
    
    >>>ad is ad +[]
    False
    

    Pure pythonic :)

提交回复
热议问题