LoginSignup
15
13

More than 5 years have passed since last update.

Pythonは大人の言語

Last updated at Posted at 2016-08-12

Python言語をオブジェクト指向の観点からみたとき、クラスメンバのprivateアクセスを強制する(クラス外部から隠蔽する)言語機能が存在しない。慣習的にメンバ名先頭にアンダースコア(_)を1つまたは2つ付けることで、それがprivate/外部からアクセスしてはいけないことを表明するが、それらのアクセスを完全に禁止することはできない。

"We are all (consenting) adults here"
-- Guido van Rossum氏(Python言語の作者)もしくはPythonコミュニティー

class Person:

  # プライベート関数(_が1個)
  def _privateFun(self):
    print("private!")

  # プライベート関数(_が2個)
  def __morePrivateFun(self):
    print("secret!")


target = Person()

target._privateFun()   # OK

target.__morePrivateFun()         # これはNGだが
target._Person__morePrivateFun()  # OK
15
13
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
13