「自己」は、クラスの現在のオブジェクト インスタンスの従来のプレースホルダーです。オブジェクトのプロパティ、フィールド、またはクラス内のメソッドを、「それ自体」を参照しているかのように参照する場合に使用されます。しかし、短くするために、Python プログラミング領域の誰かが "self" を使い始めました。他の領域では "this" を使用していますが、置き換えられないキーワードとして作成されています。コードの可読性を高めるために、むしろ「その」を使用しました。これは Python の優れた点の 1 つです。"self" 以外のオブジェクトのインスタンスに対して、独自のプレースホルダーを自由に選択できます。自己の例:
class UserAccount():
def __init__(self, user_type, username, password):
self.user_type = user_type
self.username = username
self.password = encrypt(password)
def get_password(self):
return decrypt(self.password)
def set_password(self, password):
self.password = encrypt(password)
ここで、「self」を「its」に置き換えます。
class UserAccount():
def __init__(its, user_type, username, password):
its.user_type = user_type
its.username = username
its.password = encrypt(password)
def get_password(its):
return decrypt(its.password)
def set_password(its, password):
its.password = encrypt(password)
今より読みやすいのはどれですか?