Pythonの中の単下線、二重下線、二頭下線の違い.

2108 ワード

>>> 
    class student(object):
    __name = 0
    _sex = 'male'


>>> student.__dict__
mappingproxy({'__module__': '__main__', '_student__name': 0, '_sex': 'male', '__dict__': '__dict__' of 'student' objects>, '__weakref__': '__weakref__' of 'student' objects>, '__doc__': None})

>>> student.__name
Traceback (most recent call last):
  File "", line 1, in 
    student.__name
AttributeError: type object 'student' has no attribute '__name'

>>> student._student__name

>>> student._sex
'male'

まじめに見ると、細心の注意を払うと問題の所在がわかります.
      :          ,                   ,          ,    :             ,  __dict__    ,     ,Python   __name     _student__name,      _student__name  ,    。  ,_name    。  :

     :Python       ,         

    :      

    :        :_  __   ,      ,              _  。