New submission from mastahyeti:

This patch removes the inheritance from namedtuple and attempts to add the 
necessary methods to make it backwards compatible.

When parsing a url with urlparse.urlparse, the return type is non-mutable 
(named tuple). This is really inconvenient, because one of the most common 
(imop) use cases for urlparse is to parse a url, make an adjustment or change 
and then unparse it. Currently, something like this is required:

    import urlparse

    url = list(urlparse.urlparse('http://www.example.com/foo/bar?hehe=haha'))
    url[1] = 'python.com'
    new_url = urllib.urlunparse(url)

I think this is really clunky. Moving to a mutable return type is challenging 
because (to my knowledge) there are no types that are mutable and compatible 
with tuple. This patch removes the inheritance from namedtuple and attempts to 
add the necessary methods to make it backwards compatible. Does any one know of 
a better way to do this? It would be nice if there were a namedlist type that 
acted like namedtuple but was mutable. 

With these updates, urlparse can be used as follows:

    import urlparse

    url = list(urlparse.urlparse('http://www.example.com/foo/bar?hehe=haha'))
    url.netloc = 'www.python.com'
    urlparse.urlunparse(url)

I think this is much better. Let me know if you disagree...

Also, I ran the script through autopep8 because it was messy.

Also, I'm not sure if I'm supposed to duplicate this patch over to Python3. I 
can do that if necessary

----------
components: Library (Lib)
files: urlparse_patch.patch
keywords: patch
messages: 169474
nosy: mastahyeti
priority: normal
severity: normal
status: open
title: mutable urlparse return type
type: enhancement
versions: Python 2.7
Added file: http://bugs.python.org/file27063/urlparse_patch.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15824>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to