initialization in argument definitions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brentt

    initialization in argument definitions

    Hi, I know this is a terribly simple question, but the docs seem to be
    designed for people who probably find a the answer to this question
    terribly obvious. But its not at all obvious to me.

    I can't figure out why when I define a function, a variable
    (specifically a list) that I define and initialize in the argument
    definitions, will not initialize itself every time its called. So for
    example, when making a simple list of a counting sequence from num (a
    range list), if I call the function multiple times, it appends the
    elements to the list generated the times it was called before, even
    though the variable for the list is initialized in the argument
    definitions.

    def foo_range(num,a List = []):
    aList = []
    #why is this seemingly extra initialization necessary? shouldn't it be
    initialized in the argument definitions?
    #but if its not there and the function is called multiple times the
    elements generated (see below)
    #append to the list generated before.
    while num <= 10:
    aList.append(nu m)
    num +=1
    else:
    return aList

    Why is this? Thanks, hope its not a stupid quesiton.
  • Chris Rebert

    #2
    Re: initialization in argument definitions

    On Fri, Nov 21, 2008 at 1:25 PM, Brentt <BrenttNewman@g mail.comwrote:
    Hi, I know this is a terribly simple question, but the docs seem to be
    designed for people who probably find a the answer to this question
    terribly obvious. But its not at all obvious to me.
    >
    I can't figure out why when I define a function, a variable
    (specifically a list) that I define and initialize in the argument
    definitions, will not initialize itself every time its called. So for
    example, when making a simple list of a counting sequence from num (a
    range list), if I call the function multiple times, it appends the
    elements to the list generated the times it was called before, even
    though the variable for the list is initialized in the argument
    definitions.
    >
    def foo_range(num,a List = []):
    That should probably be: def foo_range(num, aList=None):
    aList = []
    #why is this seemingly extra initialization necessary? shouldn't it be
    initialized in the argument definitions?
    #but if its not there and the function is called multiple times the
    elements generated (see below)
    #append to the list generated before.
    while num <= 10:
    aList.append(nu m)
    num +=1
    else:
    return aList
    >
    Why is this? Thanks, hope its not a stupid quesiton.
    No, but it is a very frequently asked one:


    Cheers,
    Chris
    --
    Follow the path of the Iguana...

    Comment

    • George Sakkis

      #3
      Re: initialization in argument definitions

      On Nov 21, 4:25 pm, Brentt <BrenttNew...@g mail.comwrote:
      Hi, I know this is a terribly simple question, but the docs seem to be
      designed for people who probably find a the answer to this question
      terribly obvious. But its not at all obvious to me.
      Don't worry, it's not obvious to *anyone* new to Python (and many not-
      so-new for that matter).
      I can't figure out why when I define a function, a variable
      (specifically a list) that I define and initialize in the argument
      definitions, will not initialize itself every time its called. So for
      example, when making a simple list of a counting sequence from num (a
      range list), if I call the function multiple times, it appends the
      elements to the list generated the times it was called before, even
      though the variable for the list is initialized in the argument
      definitions.
      >
      def foo_range(num,a List = []):
      aList = []
      #why is this seemingly extra initialization necessary? shouldn't it be
      initialized in the argument definitions?
      #but if its not there and the function is called multiple times the
      elements generated (see below)
      #append to the list generated before.
      while num <= 10:
      aList.append(nu m)
      num +=1
      else:
      return aList
      >
      Why is this? Thanks, hope its not a stupid quesiton.
      Sigh.. no it's not stupid at all; actually it is (and will probably
      remain, unfortunately) the most FAQ of all times:
      Contents: General Python FAQ- General Information- What is Python?, What is the Python Software Foundation?, Are there copyright restrictions on the use of Python?, Why was Python created in the fi...


      George

      Comment

      • John Machin

        #4
        Re: initialization in argument definitions

        On Nov 22, 8:25 am, Brentt <BrenttNew...@g mail.comwrote:
        Hi, I know this is a terribly simple question, but the docs seem to be
        designed for people who probably find a the answer to this question
        terribly obvious. But its not at all obvious to me.
        >
        I can't figure out why when I define a function, a variable
        (specifically a list) that I define and initialize in the argument
        definitions, will not initialize itself every time its called.
        Have you worked through the tutorial (which is part of the docs)? See
        As well as the while statement just introduced, Python uses a few more that we will encounter in this chapter. if Statements: Perhaps the most well-known statement type is the if statement. For exa...

        .... look for "Important warning"

        Comment

        • Terry Reedy

          #5
          Re: initialization in argument definitions

          Brentt wrote:
          Hi, I know this is a terribly simple question, but the docs seem to be
          designed for people who probably find a the answer to this question
          terribly obvious. But its not at all obvious to me.
          >
          I can't figure out why when I define a function, a variable
          (specifically a list) that I define and initialize in the argument
          definitions, will not initialize itself every time its called. So for
          example, when making a simple list of a counting sequence from num (a
          range list), if I call the function multiple times, it appends the
          elements to the list generated the times it was called before, even
          though the variable for the list is initialized in the argument
          definitions.
          >
          def foo_range(num,a List = []):
          aList = []
          #why is this seemingly extra initialization necessary? shouldn't it be
          initialized in the argument definitions?
          #but if its not there and the function is called multiple times the
          elements generated (see below)
          #append to the list generated before.
          while num <= 10:
          aList.append(nu m)
          num +=1
          else:
          return aList
          >
          Why is this? Thanks, hope its not a stupid quesiton.
          The def statement is an executable statement that creates a function
          object. When you execute the def statement, the parameter list,
          including default arg object expressions, is evaluated for creating the
          function object and its associated code object. The 'suite' that
          follows the ':' and possible a doc string is compiled for the code object.

          The language manual entry for Function definitions explains this, with
          the first sentence in bold.
          "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 that 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."

          The allows a choice of having a 'default' evaluated either when the
          function is defined, which is nearly always what is wanted, or when the
          function is called, by using None or another flag object, and then testing.

          tjr

          Comment

          • Egon Frerich

            #6
            Re: initialization in argument definitions

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            Brentt schrieb:
            | Hi, I know this is a terribly simple question, but the docs seem to be
            | designed for people who probably find a the answer to this question
            | terribly obvious. But its not at all obvious to me.
            |
            | I can't figure out why when I define a function, a variable
            | (specifically a list) that I define and initialize in the argument
            | definitions, will not initialize itself every time its called. So for
            | example, when making a simple list of a counting sequence from num (a
            | range list), if I call the function multiple times, it appends the
            | elements to the list generated the times it was called before, even
            | though the variable for the list is initialized in the argument
            | definitions.
            |
            | def foo_range(num,a List = []):
            | aList = []
            | #why is this seemingly extra initialization necessary? shouldn't it be
            | initialized in the argument definitions?
            | #but if its not there and the function is called multiple times the
            | elements generated (see below)
            | #append to the list generated before.
            | while num <= 10:
            | aList.append(nu m)
            | num +=1
            | else:
            | return aList
            |
            | Why is this? Thanks, hope its not a stupid quesiton.



            look up
            As well as the while statement just introduced, Python uses a few more that we will encounter in this chapter. if Statements: Perhaps the most well-known statement type is the if statement. For exa...


            "The execution of a function introduces a new symbol table used for
            the local variables of the function. More precisely, all variable
            assignments in a function store the value in the local symbol table;
            whereas variable references first look in the local symbol table, then
            in the local symbol tables of enclosing functions, then in the global
            symbol table, and finally in the table of built-in names. Thus, global
            variables cannot be directly assigned a value within a function (unless
            named in a global statement), although they may be referenced."

            Egon

            | --
            | http://mail.python.org/mailman/listinfo/python-list

            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.6 (GNU/Linux)
            Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

            iD8DBQFJJz2iZRi Do9Iq4qIRAhTLAJ 41TTysvN++TNF1t vanjAxBPhdBawCf e1tY
            uDdbPYWBAEkbYNh bKQGkx88=
            =9gL8
            -----END PGP SIGNATURE-----

            Comment

            • Steven D'Aprano

              #7
              Re: initialization in argument definitions

              On Fri, 21 Nov 2008 13:25:45 -0800, Brentt wrote:
              I can't figure out why when I define a function, a variable
              (specifically a list) that I define and initialize in the argument
              definitions, will not initialize itself every time its called.
              Because you haven't told the function to initialize the value every time
              it's called. You are laboring under a misapprehension . Function default
              values are created ONCE, when you define the function.
              So for
              example, when making a simple list of a counting sequence from num (a
              range list), if I call the function multiple times, it appends the
              elements to the list generated the times it was called before, even
              though the variable for the list is initialized in the argument
              definitions.
              No it isn't. You need to re-set your thinking, that's not what Python
              does. Try this:

              def expensive():
              # simulate an expensive function call
              import time
              time.sleep(30)
              return time.time()


              def parrot(x=expens ive()):
              return x

              The expensive call is made once only. If you want it made every time, you
              have to explicitly make that call every time:

              def parrot(x=None):
              if x is None:
              x = expensive()
              return x


              For bonus marks, predict the behaviour of this:

              def spam():
              def ham(x=expensive ()):
              return x
              return ham()



              --
              Steven

              Comment

              • Terry Reedy

                #8
                Re: initialization in argument definitions

                George Sakkis wrote:
                On Nov 21, 4:25 pm, Brentt <BrenttNew...@g mail.comwrote:
                >
                >Hi, I know this is a terribly simple question, but the docs seem to be
                >designed for people who probably find a the answer to this question
                >terribly obvious. But its not at all obvious to me.
                >
                Don't worry, it's not obvious to *anyone* new to Python (and many not-
                so-new for that matter).
                Speak for yourself, not for me.

                Comment

                Working...