pop() clarification

S

Scott

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().
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() ) is the only format to use when removing a
specific element because......with 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'['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.
 
J

Jerry Hill

I see says, "When removing a specific element from a list using pop() it
must be in this format: list.pop().


The tutorial (http://docs.python.org/tut/node7.html) says the following:

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. (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?
 
F

Facundo Batista

Scott said:
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().


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() 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:

http://www.python.org/doc/current/lib/typesseq-mutable.html

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'['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,
 
7

7stud

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().
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
 
B

Bruno Desthuilliers

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'

['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().


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:
http://en.wikipedia.org/wiki/Backus-Naur_form

And some informations about the variant used in Python's doc here:
http://docs.python.org/ref/notation.html

HTH
 
C

Carl Banks

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'

['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().
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
 
S

sjdevnull

Carl said:
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().
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.
 
C

Christophe

Carl Banks 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().
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top