python属性访问权限_Python类的访问权限(public、 private、protected)

与 C++、C#、Java 等语言相似,Python 支持将类的属性和方法设置成特定的访问权限,但不是通过关键字区分,而是使用一套约定式的规则:

使用一个下画线“_”开头的属性或方法为保护(protected)属性或方法,只能在类或其派生类中访问,在类内部以“self._属性名或方法名”的方式使用;

其他的属性或方法为公有(public)属性或方法,可在类的外部直接访问,在类内部以“self.属性名或方法名”的方式使用。

以下例子展示了三种不同访问权限的属性和方法:

class Class1:

public1= 111

_protected1 = 222

_private1 = 333

def publicFunc1(self):

pass

def _protectedFunc1(self):

pass

def __privateFunc1(self):

pass

class Class2(Class1):

public2 = 444

_protected2 = 555

__private2 = 666

def publicFunc2(self):

pass

def _protectedFunc2(self):

pass

def __privateFunc2(self):

pass

c1 = Class1()

print(c1.public1)

print(c1._protected1)

print(c1.__private1)

c1.publicFunc1()

c1._protectedFunc1()

c1.__privateFunc1()

c2 = Class2()

print(c2.public1)

print(c2._protected1)

print(c2.__private1)

print (c2.public2)

print(c2._protected2)

print(c2.__private2)

c2.publicFunc1()

c2._protectedFunc1()

c2.__privateFunc1()

c2.publicFunc2()

c2._protectedFunc2()

c2.__privateFunc2()

上述代码的运行结果如下所示:

>>> class Class1:

...          public1= 111

...          protected1 = 222

...          _private1 = 333

...          def publicFunc1(self):

...              pass

...          def _protectedFunc1(self):

...              pass

...          def __privateFunc1(self):

...              pass

>>> class Class2(Class1):

...          public2 = 444

...          _protected2 = 555

...          __private2 = 666

...          def publicFunc2(self):

...              pass

...          def _protectedFunc2(self):

...              pass

...          def __privateFunc2(self):

...              pass

>>> c1 = Class1()

>>> print(c1.public1)

111

>>> print(c1._protected1)

222

>>> print(c1.__private1)

Traceback (most recent call last):

File "", line 1, in

print(c1.__private1)

AttributeError: 'Class1' object has no attribute '__private1'

>>> c2 = Class2()

>>> print(c2.public1)

111

>>> print(c2._protected1)

222

>>> print(c2.__private1)

Traceback (most recent call last):

File "", line 1, in

print(c2.__private1)

AttributeError: 'Class2' object has no attribute '__private1'

>>> print (c2.public2)

444

>>> print(c2._protected2)

555

>>> print(c2.__private2)

Traceback (most recent call last):

File "", line 1, in

print(c2.__private2)

AttributeError: 'Class2' object has no attribute '__private2'

>>> c2.publicFunc1()

>>> c2._protectedFunc1()

>>> c2.__privateFunc1()

Traceback (most recent call last):

File "", line 1, in

c2.__privateFunc1()

AttributeError: 'Class2' object has no attribute '__privateFunc1'

>>> c2.publicFunc2()

>>> c2._protectedFunc2()

>>> c2.__privateFunc2()

Traceback (most recent call last):

File "", line 1, in

c2.__privateFunc2()

AttributeError: 'Class2' object has no attribute '__privateFunc2'

可以看到,在外部直接访问类的私有属性或方法时触发了 AttributeError 异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值