473,414 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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......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'
>>>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.
Apr 11 '07 #1
7 24914
On 4/11/07, Scott <s_*********@comcast.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
Apr 11 '07 #2
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:

http://www.python.org/doc/current/li...q-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'
>>>>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/
Apr 11 '07 #3
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:
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
Apr 11 '07 #4
On Apr 11, 10:44 am, "Scott" <s_brosci...@comcast.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
Apr 11 '07 #5
On Apr 11, 3:10 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
On Apr 11, 10:44 am, "Scott" <s_brosci...@comcast.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

Apr 12 '07 #6
Carl Banks wrote:
On Apr 11, 3:10 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
On Apr 11, 10:44 am, "Scott" <s_brosci...@comcast.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.

Apr 12 '07 #7
Carl Banks a écrit :
On Apr 11, 3:10 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
>On Apr 11, 10:44 am, "Scott" <s_brosci...@comcast.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.
Apr 12 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Nigel Mercier ® | last post by:
I'm just starting to learn JavaScript, so far I like it! I want to add some JS to my Ebay listings, to manually open a pop-up window. It works OK, but the JS gets rejected by Ebay. If I can't...
53
by: Bill | last post by:
Hello Programmers, I am looking for either Java Script (OR HTML etc) to DEFEAT Pop-up Stoppers e.g It will bring up a window that will LOOK like a Pop-up FEEL like a Pop-up Allow a name and...
18
by: Mickey Segal | last post by:
On comp.lang.java.programmer we are discussing problems created for Java programs by pop-up blockers (in the thread "showDocument blocked by new microsoft pop-up blocker"). Our problem is that...
4
by: nsr93 | last post by:
I am not sure if this was the proper group to post this, but here is my question: I am a Java consultant. I have new client I am working for to make a web based application similar to an...
10
by: Madame Blablavatsky | last post by:
hello, i have a few links on a page. when one clicks a link an pop up window with an image opens. when people click an other link i want the already availible pop up window to close before the...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
2
by: domtam | last post by:
Hi all, Here is what I want: when a user clicks a <asp:button>, its event handler will do some server-side processing and then pop up a windows of another page. As far as I know, if no pop-up...
25
by: Nicholas Parsons | last post by:
Howdy Folks, I was just playing around in IDLE at the interactive prompt and typed in dir({}) for the fun of it. I was quite surprised to see a pop method defined there. I mean is that a...
2
by: ravir | last post by:
Hi, I am new to this group. I am working in Perl and shellscripts. I have a clarification regarding perl grep and pattern matching. I am writing a perl script to automate the process of code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.