[Python-Dev] unittest argv

Guido van Rossum guido at python.org
Mon May 1 20:31:00 CEST 2006


On 5/1/06, John Keyes <john at integralsource.com> wrote:
> > No. Late binding of sys.argv is very important. There are plenty of
> > uses where sys.argv is dynamically modified.
>
> Can you explain this some more?  If it all happens in the same
> function call so how can it be late binding?

You seem to be unaware of the fact that defaults are computed once,
when the 'def' is executed (typically when the module is imported).

Consider module A containing this code:

  import sys
  def foo(argv=sys.argv):
      print argv

and module B doing

  import sys
  import A
  sys.argv = ["a", "b", "c"]
  A.foo()

This will print the initial value for sys.argv, not ["a", "b", "c"].
With the late binding version it will print ["a", "b", "c"]:

  def foo(argv=None):
    if argv is None:
      argv = sys.argv
    print argv

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list