urllib.urlencode has 2 parameters??

Steve Holden sholden at holdenweb.com
Wed Nov 21 15:17:25 CET 2001


"Matthew D. Wood" <woodm at equire.com> wrote in message
news:32cedc66.0111202313.1480ee99 at posting.google.com...
> I understand that there is documentation for the vast majority of the
> python modules.  In point of fact, I stated that I had been looking at
> the documentation for urlencode in my original post.
> I don't understand what the documentation is trying to say.
> "If any values in the query arg are sequences..."  Isn't query
> supposed to be a sequence to begin with?  (I know that it can also be
> a dict.)  I even tried it with a list of tuples, which is a sequence
> of sequences, nothing changed when I changed the parameter from 1 to
> 0.  I don't get it.
> My hope was that someone else had struggled through what I had, and
> that everyone (myself especailly) could benefit from their time.
> In short, I know that I should always RTFM, I just don't understand
> what TFM is saying.
> > On 20-Nov-2001 Matthew D. Wood wrote:
> > > I've been staring at the documentation page for urllib for about 15
> > > minutes, and experimenting around on the python-command-line trying to
> > > figure out what the second parameter for urllib.urlencode does.
> >
> > def urlencode(query, doseq=0):
> > If any values in the query arg are sequences and doseq is true, each
> >     sequence element is converted to a separate parameter.
> >
> > That is from urllib.urlencode.__doc__.  Also, most of python's modules
are in
> > python so you can just read the urllib.py file and it is explained there
> > comments and code.
> >
Well, remember how web browsers will encode a form that contains multiple
values for the same name (for example, suppose you have several text inputs
all called "two")? They send a single value that represents the value set as
comma-separated values in a single value string. urllib offers to do a
similar conversion on the way out if you provide sequences of values rather
than single values. But you have to tell it to do that:

>>> ue([("one", 1), ("two", (1,2,3))])
'one=1&two=%281%2C+2%2C+3%29' # encoded repr((1,2,3))
>>> ue([("one", 1), ("two", (1,2,3))], doseq=1)
'one=1&two=1&two=2&two=3' # separate values for each sequence element

It's a pretty esoteric thing. Why are you worrying about it?!?!


More information about the Python-list mailing list