Python - 私有方法,专有方法

来源:互联网 发布:好的监控软件 编辑:程序博客网 时间:2024/06/14 16:42

Python的私有方法:以'__'双划线开头,但不以双划线结尾, __privateMethod

专有方法:以双划线开头和结尾, __init__

e.g

class Person:    def __init__(self,name):        self.name = "Person"    def __getName(self):        return self.name

<pre style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-size: 9pt; background-color: rgb(255, 255, 255);"><span style="background-color: rgb(255, 228, 255);">aPerson</span> = Person(<span style="color: rgb(0, 128, 0); font-weight: bold;">'test'</span>)<span style="background-color: rgb(228, 228, 255);">aPerson</span>.getName()

结果: aPerson.getName()
AttributeError: Person instance has no attribute 'getName', 因为是私有所以会报错哦。

而__init__就是一个初始化函数,典型的专有函数


0 0
原创粉丝点击