Skip to content

Instantly share code, notes, and snippets.

@ryu22e
Created September 8, 2022 08:07
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/a92b797eb24fe2fe00abbc93a2b80b61 to your computer and use it in GitHub Desktop.
Save ryu22e/a92b797eb24fe2fe00abbc93a2b80b61 to your computer and use it in GitHub Desktop.
Pythonで先頭アンスコ2つのメソッドの仕様例
class Base:
def _get_name1(self):
return "Base"
def __get_name2(self):
return "Base"
def say_name1(self):
print("My name is " + self._get_name1())
def say_name2(self):
print("My name is " + self.__get_name2())
class Taro(Base):
def _get_name1(self):
# アンスコ1つだとオーバーライドする
return "Taro"
def __get_name2(self):
# アンスコ2つだとオーバーライドしない
return "Taro"
taro = Taro()
taro.say_name1() # My name is Taro
taro.say_name2() # My name is Base
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment