Skip to content

Instantly share code, notes, and snippets.

@ryu22e
Last active September 8, 2022 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryu22e/246966fecfd82d93cc8e44eac4888a29 to your computer and use it in GitHub Desktop.
Save ryu22e/246966fecfd82d93cc8e44eac4888a29 to your computer and use it in GitHub Desktop.
Pythonで先頭アンスコ2つのメソッドはどんな名前になっているのか検証
>>> class Example:
...     def __hello(self):
...         print("Hello!")
...
>>> example = Example()
>>> dir(example)
['_Example__hello', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> example._Example__hello()  # 先頭アンスコ2つメソッド名は強制的に頭にクラス名が付く
Hello!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment