6

In Python 2.7.1, I'm trying to provide a list of messages as the first argument, and a list of colors as the second argument. I want the second argument to default to a list of whites if it's not provided. This is the way I tried to do it:

def multicolor_message(msgs, colors=[libtcod.white for x in len(msgs)]):
#function body

libtcod.white is a part of the library I'm using and is in no way causing any problems. The compiler says the variable msgs is not defined. Obviously the msgs variable doesn't exist in this scope, but I need to create a list of appropriate length and assign it to colors. What is the cleanest way to do this?

boobookum
  • 113
  • 2
  • 6

2 Answers2

6

I would do it like this:

def multicolor_message(msgs, colors=None):
  if colors is None:
    colors=[libtcod.white for x in len(msgs)]
piokuc
  • 25,594
  • 11
  • 72
  • 102
  • 4
    And the reason for doing it like this is that you can't reference other arguments of the same function in the default values. Default values are evaluated in the function's defining scope, and the arguments themselves only exist once the function is called. – Matti Virkkunen Dec 31 '12 at 13:07
  • 2
    Another important reason is that [mutable default arguments](http://stackoverflow.com/q/1132941/395760) rarely do what you want. –  Dec 31 '12 at 13:28
4

That's not possible is python, as function's default arguments are executed at function definition time and your msgs variable will not be available until the function is called.

From the docs:

Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g.:

def whats_on_the_telly(penguin=None):
    if penguin is None:
        penguin = []
    penguin.append("property of the zoo")
    return penguin
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Not at compile time. At function definition time. The difference matters for nested functions, or when module-level names are used in the expressions. –  Dec 31 '12 at 13:26