有什么比命名元组 _replace 更好的替代品?
采用以下代码:
>>> 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)
有点愚蠢。 :)
有想法吗?
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 as it is an underscored method, it's supposed to be internal, and not used. Is there an alternative? I don't want to do:
>>> parts = parts[:2] + ('/3.0'+parts.path,) + parts[3:]
Because that makes it an ordinary tuple, and not a namedtuple, and doing:
>>> parts = namedtuple(scheme=parts.scheme, netloc=parts.netloc, etc etc)
is kinda stupid. :)
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
namedtuple
的方法之所以以下划线开头只是为了防止名称冲突。它们不应被视为仅供内部使用:The reason methods of
namedtuple
start with an initial underscore is only to prevent name collisions. They should not be considered to be for internal use only: