0
点赞
收藏
分享

微信扫一扫

【Python】如何正确使用下划线 underscore

浮游图灵 2022-02-14 阅读 93

Underscore Naming

Single Leading Underscore:

  • _var

  • Indicating a name is protected, for internal use. Not enforced by python interpreter


Double Leading Underscore:

  • __var
  • Indicating a name is private. Enforced by interpreter, attempt to call it will trigger AttributeError

Double Leading Trailing Underscore:

  • __var__
  • Special methods (magic methods) defined by python language, avoid naming your own attribute

Single Trailing Underscore:

  • var_
  • Avoid naming conflict with python keywords

Single Underscore:

  • _
  • Temporary variable name, variables that are never used
  • Example:
a, b, c, d = ([] for _ in range(4))

for _ in random_list:
    some_operation()

Reference

Dan Bader - The Meaning of Underscores in Python

Tutorials Teacher - Python - Public, Protected, Private Members

举报

相关推荐

0 条评论