pop() clarification

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

    #1

    pop() clarification

    As said before I'm new to programming, and I need in depth explaination to
    understand everything the way I want to know it, call it a personality quirk
    ;p.

    With pop() you remove the last element of a list and return its value:

    Now I know list is a bad name, but for the sake of arguement lets assume its
    not a built in sequence>
    >>>list = ['this', 'is', 'an', 'example']
    >>>list.pop()
    'example'
    >>>list
    ['this', 'is', 'an']

    I understand all that. What I don't understand is why all the documentation
    I see says, "When removing a specific element from a list using pop() it
    must be in this format: list.pop([i]).
    At first I took that to mean that list.pop(i) would return some type of
    error, but it doesn't.
    I can't find any documentation saying that this rule that I keep reading
    about (again list.pop([i]) ) is the only format to use when removing a
    specific element because......wi th the explaination to follow.

    Now I'm not stupid enough to believe that I'm the first to try:
    >>>list = ['this', 'is', 'an', 'example']
    >>>list.pop(1 )
    and have it return the desired effect of:
    'is'
    >>>list
    ['this', 'an', 'example']


    I guess simplistically all I'm asking is: Is this just a community agreed
    upon rule for readability purposes? or Is it a rule that's in place for a
    reason I'll learn later on? Please keep in mind my intro sentence to this
    post. I would like a very detailed explaination, or at least a link to a
    very detailed expression. ONLY....of course....if the explaination isn't,
    well it just is because we all agreed to use it that way. That explaination
    I'll understand and except, but with a bit of dissatisfaction .


  • Jerry Hill

    #2
    Re: pop() clarification

    On 4/11/07, Scott <s_broscious@co mcast.netwrote:
    I see says, "When removing a specific element from a list using pop() it
    must be in this format: list.pop([i]).
    The tutorial (http://docs.python.org/tut/node7.html) says the following:

    pop( [i] )
    Remove the item at the given position in the list, and return it.
    If no index is specified, a.pop() removes and returns the last item in
    the list. (The square brackets around the i in the method signature
    denote that the parameter is optional, not that you should type square
    brackets at that position. You will see this notation frequently in
    the Python Library Reference.)

    Does that answer your question?

    --
    Jerry

    Comment

    • Facundo Batista

      #3
      Re: pop() clarification

      Scott wrote:
      Now I know list is a bad name, but for the sake of arguement lets assume its
      not a built in sequence>
      It's easier to use another name, than writing all that parragraph, ;)

      I understand all that. What I don't understand is why all the documentation
      I see says, "When removing a specific element from a list using pop() it
      must be in this format: list.pop([i]).
      You're reading it everywhere because it's the correct syntax, ;)
      At first I took that to mean that list.pop(i) would return some type of
      error, but it doesn't.
      "i" is enclosed in square brackets because it's optional. So,
      list.pop(i) is a correct way to call it, as correct as list.pop().

      Take note that list.pop([i]) is *not* the correct way to call it.

      I assume you're getting confused by the [], asuming that those means a
      list. No. Take a look here:



      As you see there, when an iterable is needed, it just named different,
      syntax do not use brackets to mean "list".

      Now I'm not stupid enough to believe that I'm the first to try:
      >>>>list = ['this', 'is', 'an', 'example']
      >>>>list.pop( 1)
      and have it return the desired effect of:
      'is'
      >>>>list
      ['this', 'an', 'example']
      But try this also:
      >>l = ['this', 'is', 'an', 'example']
      >>l.pop([2])
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      TypeError: an integer is required

      Regards,

      --
      .. Facundo
      ..
      Blog: http://www.taniquetil.com.ar/plog/
      PyAr: http://www.python.org/ar/


      Comment

      • Bruno Desthuilliers

        #4
        Re: pop() clarification

        Scott a écrit :
        As said before I'm new to programming, and I need in depth explaination to
        understand everything the way I want to know it, call it a personality quirk
        ;p.
        >
        With pop() you remove the last element of a list and return its value:
        >
        Now I know list is a bad name, but for the sake of arguement lets assume its
        not a built in sequence>
        >
        >>>>list = ['this', 'is', 'an', 'example']
        >>>>list.pop( )
        >
        'example'
        >
        >>>>list
        >
        ['this', 'is', 'an']
        >
        I understand all that. What I don't understand is why all the documentation
        I see says, "When removing a specific element from a list using pop() it
        must be in this format: list.pop([i]).
        There are conventions (not specific to Python) about how to describe
        languages syntax (including functions signatures). One of these
        conventions is that things between square brackets are optional. So when
        you see arguments between square brackets in the *documentation* of a
        function, that means these arguments are optional. Here, that means that
        list.pop takes one optional argument which is the index of the element
        to pop. It defaults to the last element, but nothing prevents you from
        specifying any other correct index.
        I guess simplistically all I'm asking is: Is this just a community agreed
        upon rule for readability purposes?
        It's a (more or less) standard syntax for such things, that you'll find
        for almost any programming language. For a function taking 2 optionals
        arguments, you'd see:

        some_function([opt_arg1 [,opt_arg2]])

        Which means that some_function accepts two optional arguments, and that
        you must pass the first if you also want to pass the second
        or Is it a rule that's in place for a
        reason I'll learn later on? Please keep in mind my intro sentence to this
        post. I would like a very detailed explaination, or at least a link to a
        very detailed expression.
        Languages have a grammar and syntax, and one needs to be able to
        describe it formally. The usual formalism is some variation of the BNF
        (Backus-Naur Form) notation. You'll find detailed explanations about BNF
        here:


        And some informations about the variant used in Python's doc here:


        HTH

        Comment

        • 7stud

          #5
          Re: pop() clarification

          On Apr 11, 10:44 am, "Scott" <s_brosci...@co mcast.netwrote:
          As said before I'm new to programming, and I need in depth explaination to
          understand everything the way I want to know it, call it a personality quirk
          ;p.
          >
          With pop() you remove the last element of a list and return its value:
          >
          Now I know list is a bad name, but for the sake of arguement lets assume its
          not a built in sequence>
          >
          >>list = ['this', 'is', 'an', 'example']
          >>list.pop()
          'example'
          >>list
          >
          ['this', 'is', 'an']
          >
          I understand all that. What I don't understand is why all the documentation
          I see says, "When removing a specific element from a list using pop() it
          must be in this format: list.pop([i]).
          At first I took that to mean that list.pop(i) would return some type of
          error, but it doesn't.
          It's understandable that the definition of pop() is confusing in that
          way. It looks like the argument should be a list. As others have
          said, that is not what the brackets mean when the documents show the
          formal definition of a function. A clearer example might be a
          function definition that requires some mandatory arguments and has
          some optional arguments:

          dict.get(key[, default])

          That shows that the get() function requires one mandatory argument
          "key" and that you can also send it one optional argument "default".
          If a function were to require a list as an argument, it's definition
          would be written something like this:

          somefunc(aList) -- where 'aList' is a list of integers


          Comment

          • Carl Banks

            #6
            Re: pop() clarification

            On Apr 11, 3:10 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
            On Apr 11, 10:44 am, "Scott" <s_brosci...@co mcast.netwrote:
            >
            >
            >
            As said before I'm new to programming, and I need in depth explaination to
            understand everything the way I want to know it, call it a personality quirk
            ;p.
            >
            With pop() you remove the last element of a list and return its value:
            >
            Now I know list is a bad name, but for the sake of arguement lets assume its
            not a built in sequence>
            >
            >>>list = ['this', 'is', 'an', 'example']
            >>>list.pop()
            'example'
            >>>list
            >
            ['this', 'is', 'an']
            >
            I understand all that. What I don't understand is why all the documentation
            I see says, "When removing a specific element from a list using pop() it
            must be in this format: list.pop([i]).
            At first I took that to mean that list.pop(i) would return some type of
            error, but it doesn't.
            >
            It's understandable that the definition of pop() is confusing in that
            way. It looks like the argument should be a list. As others have
            said, that is not what the brackets mean when the documents show the
            formal definition of a function.
            I wonder if the documentation could take advantage of Python 3000
            annotation syntax. So

            pop([x])

            would be replaced in the docs by

            pop(x: OPTIONAL)

            Just a thought, probably not a good one. The brackets are so
            pervasive that it's probably better to just let newbies be confused
            for a little bit.


            Carl Banks

            Comment

            • sjdevnull@yahoo.com

              #7
              Re: pop() clarification

              Carl Banks wrote:
              On Apr 11, 3:10 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
              On Apr 11, 10:44 am, "Scott" <s_brosci...@co mcast.netwrote:


              As said before I'm new to programming, and I need in depth explaination to
              understand everything the way I want to know it, call it a personality quirk
              ;p.
              With pop() you remove the last element of a list and return its value:
              Now I know list is a bad name, but for the sake of arguement lets assume its
              not a built in sequence>
              >>list = ['this', 'is', 'an', 'example']
              >>list.pop()
              'example'
              >>list
              ['this', 'is', 'an']
              I understand all that. What I don't understand is why all the documentation
              I see says, "When removing a specific element from a list using pop() it
              must be in this format: list.pop([i]).
              At first I took that to mean that list.pop(i) would return some type of
              error, but it doesn't.
              It's understandable that the definition of pop() is confusing in that
              way. It looks like the argument should be a list. As others have
              said, that is not what the brackets mean when the documents show the
              formal definition of a function.
              >
              I wonder if the documentation could take advantage of Python 3000
              annotation syntax. So
              >
              pop([x])
              >
              would be replaced in the docs by
              >
              pop(x: OPTIONAL)
              >
              Just a thought, probably not a good one. The brackets are so
              pervasive that it's probably better to just let newbies be confused
              for a little bit.
              Especially since [] are used for optional arguments in many non-Python
              contexts, so sticking with [] helps those coming from a background
              that uses them in such a manner.

              Comment

              • Christophe

                #8
                Re: pop() clarification

                Carl Banks a écrit :
                On Apr 11, 3:10 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
                >On Apr 11, 10:44 am, "Scott" <s_brosci...@co mcast.netwrote:
                >>
                >>
                >>
                >>As said before I'm new to programming, and I need in depth explaination to
                >>understand everything the way I want to know it, call it a personality quirk
                >>;p.
                >>With pop() you remove the last element of a list and return its value:
                >>Now I know list is a bad name, but for the sake of arguement lets assume its
                >>not a built in sequence>
                >>>>>list = ['this', 'is', 'an', 'example']
                >>>>>list.pop ()
                >>'example'
                >>>>>list
                >>['this', 'is', 'an']
                >>I understand all that. What I don't understand is why all the documentation
                >>I see says, "When removing a specific element from a list using pop() it
                >>must be in this format: list.pop([i]).
                >>At first I took that to mean that list.pop(i) would return some type of
                >>error, but it doesn't.
                >It's understandable that the definition of pop() is confusing in that
                >way. It looks like the argument should be a list. As others have
                >said, that is not what the brackets mean when the documents show the
                >formal definition of a function.
                >
                I wonder if the documentation could take advantage of Python 3000
                annotation syntax. So
                >
                pop([x])
                >
                would be replaced in the docs by
                >
                pop(x: OPTIONAL)
                >
                Just a thought, probably not a good one. The brackets are so
                pervasive that it's probably better to just let newbies be confused
                for a little bit.
                I'd rather go for the overloading syntax. ie:


                pop(i)
                pop()

                Remove the item at the given position in the list, and return it. If no
                index is specified, a.pop() removes and returns the last item in the list.

                Comment

                Working...