[Python] When is a variable passed by reference and when by value? [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    My code:

    locs = [ [1], [2] ]
    for loc in locs:
    loc = []

    print locs
    # prints => [ [1], [2] ]


    Why is loc not reference of elements of locs?

    In Python, everything is passed as reference unless explicitly copied (is this not true?)

    How does Python decide referencing and copying?

    How to do?

    def compute(ob):
    if isinstance(ob,list): return process_list(ob)
    if isinstance(ob,dict): return process_dict(ob)

    for loc in locs:
    loc = compute(loc) # What to change here to make loc a reference of actual locs iteration?

    • locs must contain the final processed response
    • I don't want to use enumerate, is it possible without it?

    Continue reading...

Compartilhe esta Página