1 Star 0 Fork 55

zj507071 / AiLearning

forked from OpenDocCN / ailearning 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
112.md 1011 Bytes
一键复制 编辑 原始数据 按行查看 历史
布客飞龙 提交于 2020-10-19 21:08 . 2020-10-19 21:08:55

共有,私有和特殊方法和属性

  • 我们之前已经见过 special 方法和属性,即以 __ 开头和结尾的方法和属性
  • 私有方法和属性,以 _ 开头,不过不是真正私有,而是可以调用的,但是不会被代码自动完成所记录(即 Tab 键之后不会显示)
  • 其他都是共有的方法和属性
  • __ 开头不以 __ 结尾的属性是更加特殊的方法,调用方式也不同:

In [1]:

class MyClass(object):
    def __init__(self):
        print "I'm special!"
    def _private(self):
        print "I'm private!"
    def public(self):
        print "I'm public!"
    def __really_special(self):
        print "I'm really special!"

In [2]:

m = MyClass()
I'm special!

In [3]:

m.public()
I'm public!

In [4]:

m._private()
I'm private!

注意调用方式:

In [5]:

m._MyClass__really_special()
I'm really special!
Python
1
https://gitee.com/zj507071/AiLearning.git
git@gitee.com:zj507071/AiLearning.git
zj507071
AiLearning
AiLearning
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891