拿这段代码:
>>> import urlparse
>>> parts = urlparse.urlparse('http://docs.python.org/library/')
>>> parts = parts._replace(path='/3.0'+parts.path)
parts._replace 有效
但由于它是带下划线的方法,因此应该是内部方法,未使用。有其他选择吗?我不想做:
>>> parts = parts[:2] + ('/3.0'+parts.path,) + parts[3:]
因为这使它成为一个普通的元组,而不是一个命名元组,并且做:
>>> parts = namedtuple(scheme=parts.scheme, netloc=parts.netloc, etc etc)
有点傻。 :)
想法?
最佳答案
namedtuple
的方法以初始下划线开头的原因只是为了防止名称冲突。他们 should not be considered to be for internal use only :
To prevent conflicts with field names, the method and attribute names start with an underscore.
关于python - 什么是 namedtuples _replace 更好的替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2213102/