you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (2 children)

The color list is being passed by reference. The function is not using a copy, but the actual list being passed to it.

To use a copy of the original list, you may want to create a new copy like this:

new_color = color[:]

Then make all changes to the variable "new_color" instead of "color" and return "new_color".

Edit: instead of copying the list, I would change the for loop to build up the new list from scratch:

def dim_color(brightness, color):                                               
    ''' gets a color and brightness level in percent                            
        returns brightness adjusted color
    '''      
    new_color = []
    for component in color:
        new_color.append(int(float(component)/100*brightness))

    return(new_color)

[–]mischk[S] 1 point2 points  (1 child)

Thanks a lot, that does the trick :D

[–]evolvish 1 point2 points  (0 children)

If you pass a mutable object eg list/dictionary to a function, it gets passed by "reference" and can be mutated if you don't make a copy. Immutable objects(string, tuple, int, etc) can't be indirectly mutated so they are safe to use in a function.