-5

In python, we could set default argument values while define a function. But the default argument values are evaluated on once. So if we change the values of such arguments in the function, the change will be accumulated during later callings.

It means a function will keep states. It's not aligned to the idea of object-oriented and will bring much many confusions.

I think it's a bad design. Do you think so?

  • 1
    This is more a discussion topic than a specific question, which is improper for this site. Please refer to the [faq](http://stackoverflow.com/faq) for question guidelines. – Jesse the Game Dec 25 '12 at 05:23
  • 2
    You might find this question interesting: http://stackoverflow.com/q/1132941/646543 The answers explain why in Python arguments are mutable, and why that was a good design decision to make. – Michael0x2a Dec 25 '12 at 05:28
  • I made a dig to try and save this question with a title edit. You could complete it with fixing the "question" at the end of the .. "question" .. (but it's likely too late anyway) –  Dec 25 '12 at 05:31
  • @pst: I think the main source of downvotes was not the title, but the last line. If it was something like "What is the reason for this design, and how do people deal with it?" it would not sound as if the questioner thinks he knows better than Guido, and it would not trigger the "this is not a polemics site" response. – Amadan Dec 25 '12 at 05:40
  • @Amadan I don't doubt it. The title change was my impetus attempt to get the last bit changed to be "constructive" .. oh well, closed it is then :) –  Dec 25 '12 at 05:42
  • I think it's a good discussion topic. As good or bad design, it depends on everyone's own feeling or thinking or view. Since the argument should be in the function's local symbol table, why herein only evaluated only once at def? It's my question. – user1837539 Dec 25 '12 at 06:18
  • There are no "good discussion topics" on Stack Overflow, since the site's purpose is not discussion. It is getting your specific, programming-related questions answered. This is why subjective (and possibly controversial) topics like "Don't you think [Python] is badly designed?" get downvoted and closed. Please read the [Stack Overflow FAQ](http://stackoverflow.com/faq) about what kinds of posts are on-topic here. – Amadan Dec 25 '12 at 06:31

1 Answers1

3

No - it just means you should understand your tools. Your way would require that a hidden procedure be stored at def time, and evaluated each time a function is called; in Python, "explicit is better than implicit" is the (purported) design principle of choice.

Since this is not a site for debate but rather for programming Q&A, here's how to do it correctly:

Instead of

def funct(array = []):

write

def funct(array = None):
    if array is None:
        array = []

The same "complaint" goes for Ruby, BTW, (and probably other languages that have default argument values), and is not limited to Python.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Since the argument should be in the function's local symbol table, why herein evaluated only once at def? It's my question. Thanks. – user1837539 Dec 25 '12 at 06:21
  • Actually, the link @Michael0x2a posted explains it much better than I could, even though I completely agree. In languages like Python, function definition is an executable statement, not a declaration. The `def` line is executed when encountered, along with evaluation of any arguments. The rest is taken as the body, and is not evaluated. – Amadan Dec 25 '12 at 06:28
  • "In languages like Python, function definition is an executable statement, not a declaration. " It could explain my question in general. Thanks. – user1837539 Dec 25 '12 at 06:38