Python : Data Encapsulation

Python : Data Encapsulation

The following table shows the different behaviour:

NameNotationBehaviour
name Public Can be accessed from inside and outside
_name Protected Like a public member, but they shouldn‘t be directly accessed from outside.
__name Private Can‘t be seen and accessed from outside

 

 

 

 

e.g. 

class Account(object): 
    counter = 0
    def __init__(self, holder, number, balance,credit_line=1500): 
        Account.counter += 1
        self.__Holder = holder 
        self.__Number = number 
        self.__Balance = balance
        self.__CreditLine = credit_line
    def __del__(self):
        Account.counter -= 1

  



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。