风格:返回元组

时间:2017-06-11 10:10:11

标签: python

对于返回元组的情况,我遇到过这种编码风格。

_, number_of_days_in_month = calendar.monthrange(year, month)

在这种情况下,monthrange会返回一个包含两个值的元组:该月第一天的工作日和月中的天数。

  1. 这种风格可以接受吗?是否在Google Python Style Guide等着名风格指南中进行了描述?

  2. 如果_已被占用,该怎么办?例如,在Django中,它用于翻译,如下所示:

    from django.utils.translation import ugettext as _
    output = _("Welcome to my site.")
    

1 个答案:

答案 0 :(得分:0)

我认为,我们应该遵循The Hitchhiker's Python指南的建议:

http://python-guide-pt-br.readthedocs.io/en/latest/writing/style/#create-an-ignored-variable

即:在这种情况下使用__。

相关问题