Python私有化是什么意思

发布时间:2021-09-09 10:30:27 作者:chen
来源:亿速云 阅读:132

这篇文章主要介绍“Python私有化是什么意思”,在日常操作中,相信很多人在Python私有化是什么意思问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python私有化是什么意思”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

通过name mangling(名字重整(目的就是以防子类意外重写基类的方法或者属性)如:_Class__object)机制就可以访问private了。

#coding=utf-8class Person(object):    def __init__(self, name, age, taste):        self.name = name        self._age = age 
        self.__taste = taste    def showperson(self):        print(self.name)        print(self._age)        print(self.__taste)    def dowork(self):        self._work()        self.__away()    def _work(self):        print('my _work')    def __away(self):        print('my __away')class Student(Person):    def construction(self, name, age, taste):        self.name = name        self._age = age 
        self.__taste = taste    def showstudent(self):        print(self.name)        print(self._age)        print(self.__taste)    @staticmethod    def testbug():        _Bug.showbug()#模块内可以访问,当from  cur_module import *时,不导入class _Bug(object):    @staticmethod    def showbug():        print("showbug")s1 = Student('jack', 25, 'football')s1.showperson()print('*'*20)#无法访问__taste,导致报错#s1.showstudent() 
s1.construction('rose', 30, 'basketball')s1.showperson()print('*'*20)s1.showstudent()print('*'*20)Student.testbug()

[图片上传失败...(image-c0ee83-1521130952917)]

总结

到此,关于“Python私有化是什么意思”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. Python类与方法的私有化
  2. python的意思是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:Python中安装redis-py和创建连接池的方法

下一篇:怎么通过重启路由的方法切换IP地址

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》