对于返回元组的情况,我遇到过这种编码风格。
_, number_of_days_in_month = calendar.monthrange(year, month)
在这种情况下,monthrange
返回一个具有两个值的元组:每月第一天的工作日和每月的天数。
这种风格可以接受吗?它是否在一些著名的风格指南中有所描述,比如 Google Python 风格指南?
如果_
已经被占用怎么办?例如,在 Django 中,它用于这样的翻译:
from django.utils.translation import ugettext as _
output = _("Welcome to my site.")
我认为,我们应该遵循 The Hitchhiker's Guide to Python 的建议:
http://python-guide-pt-br.readthedocs.io/en/latest/writing/style/#create-an-ignored-variable
即:在这种情况下使用 __。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句