What is the best alternative to namedtuples _replace?

Take this code:

>>> import urlparse >>> parts = urlparse.urlparse('http://docs.python.org/library/') >>> parts = parts._replace(path='/3.0'+parts.path) 

parts._replace works , but since this is an underlined method, it should be internal and not used. Is there an alternative? I do not want to do:

 >>> parts = parts[:2] + ('/3.0'+parts.path,) + parts[3:] 

Because this makes it a regular tuple, not a named one, and does:

 >>> parts = namedtuple(scheme=parts.scheme, netloc=parts.netloc, etc etc) 

looks stupid. :)

Ideas?

+7
python namedtuple
source share
1 answer

The namedtuple reason namedtuple begin with an initial underscore only to prevent name namedtuple . They should not be considered for internal use only :

To prevent conflicts with field names, method and attribute names begin with an underscore.

+19
source share

All Articles