我是靠谱客的博主 美丽书本,这篇文章主要介绍Python - 私有方法,专有方法,现在分享给大家,希望可以做个参考。

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__就是一个初始化函数,典型的专有函数



最后

以上就是美丽书本最近收集整理的关于Python - 私有方法,专有方法的全部内容,更多相关Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部