pop() clarification

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Apr 11 21:21:32 CEST 2007


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



More information about the Python-list mailing list