What's the advantage of a trailing underscore in Python naming?

后端 未结 3 1244
渐次进展
渐次进展 2021-01-17 21:06

I am used to naming Python arguments in this way:

my_argument=\'foo\'

what\'s the advantage if I do this instead:

my_argum         


        
相关标签:
3条回答
  • 2021-01-17 21:18

    Exactly what it gives in the PEP: it allows you to use something that would otherwise be a Python keyword.

    as_
    with_
    for_
    in_
    
    0 讨论(0)
  • 2021-01-17 21:19

    PEP8 does not recommend this naming convention, except for names that would otherwise conflict with keywords. my_argument obviously does not conflict, so there is no reason to use an underscore and PEP8 does not recommend that you do.

    0 讨论(0)
  • 2021-01-17 21:24

    Usually naming conventions like this don't have any empiric purpose in python (i.e. they don't do anything special) aside from avoiding conflict between keywords. For example, you wouldn't name a variable class would you? You'd name it class_ to avoid conflict with the built-in keyword.

    0 讨论(0)
提交回复
热议问题