【说站】python私有方法的使用注意
2024-12-22
37
python私有方法的使用注意
1、使用注意
单下划线的方法只是开发者之间的约定,解释器不做任何改变。
双下化下的方法,是私有方法,解释器会改名,改名策略和私有变量相同,【_类名__方法名】。方法变量都在类的【__dict__】中可以找到。
2、实例
class Myclass: def __init__(self,name,age=18): self.name = name self._age = age def __getname(self): return self.name def __getage(self): return self.name a = Myclass("tom") #print(a.__getname()) # AttributeError: 'Myclass' object has no attribute '__getname' #print(a.__getage()) # AttributeError: 'Myclass' object has no attribute '__getage' print(a.__dict__) # {'name': 'tom', '_age': 18} print(a.__class__.__dict__) # {'__module__': '__main__', '__init__': <function Myclass.__init__ at 0x01ABC468>, '_Myclass__getname': <function Myclass.__getname at 0x01B06150>, '_Myclass__getage': <function Myclass.__getage at 0x01B064B0>, '__dict__': <attribute '__dict__' of 'Myclass' objects>, '__weakref__': <attribute '__weakref__' of 'Myclass' objects>, '__doc__': None} print(a._Myclass__getname()) # tom
以上就是python私有方法的使用注意,希望对大家有所帮助。更多Python学习指路:python基础教程
更新于:26天前赞一波!3
相关文章
- 【说站】python数据结构堆的介绍
- 【说站】python参数调用的注意点
- 【说站】mysql有哪些建立索引的方法
- 【说站】mysql表导出的两种方法
- 【说站】python Pandas读取数据文件的优点
- 【说站】python中in和is的区分
- 【说站】python异常中常见关键字
- 【说站】python os.path.join()函数的使用
- 【说站】python如何使用skimage包提取图像
- 【说站】python confusion_matrix()是什么
- 【说站】python中os.path.join()函数是什么
- 【说站】python中有哪些比较操作
- 【说站】python字符串的用法总结
- 【说站】php方法断点如何实现
- 【说站】java类的两种引用方法
- 【说站】python列表数据如何增加和删除
- 【说站】python解释器的多种使用
- 【说站】python多行注释的方法整理
- Google翻译退出中国:带访问方法(已验证)
- 【说站】python列表有哪些特点
文章评论
评论问答