Variable name as function argument?

This question already has an answer here:

  • How do I pass a variable by reference? 23 answers

  • Have the rounding function return a value.

    def rounding(list_):
        return [round(i, 1) for i in list_]
    

    Then you can do this:

    >>> morada=[1,2.2342,4.32423,6.1231]  #an easy example
    >>> morada = rounding(morada)
    >>> morada
    [1, 2.2, 4.3, 6.1]
    

    Or if you really really wanted it to assign within the function you could do this:

    def rounding(list_):
        list_[:] = [round(i,1) for i in args]
    

    Close. Lists are mutable, so...

    name[:] = M
    

    You can use eval()

    For example, the following will start with a list containing [1, 2, 3, 4] and change the first element to 5:

    list_0 = [1, 2, 3, 4]
    
    def modify_list(arg):
      list_1 = eval(arg)
      list_1[0] = 5
    
    modify_list('list_0')
    print list_0  
    
    链接地址: http://www.djcxy.com/p/20844.html

    上一篇: 将Python字典传递给类会改变字典的值

    下一篇: 变量名称作为函数参数?