栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python中私有属性的理解

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python中私有属性的理解

面向对象的类模板中有内置属性,私有属性,固定属性,普通属性
内置属性:类中以__开头和以__结尾的变量或者方法都是内置属性
私有属性:类中以__开头的变量或者方法都是私有属性
固定属性:__slots__标志的就是固定变量属性
普通属性:除了上面的三种,剩下的都是普通属性.

下面来介绍私有属性:
一.私有属性的访问:在类外部不能被使用,但是可以通过不正常的方式去访问.

class Person(object):
    __country = 'CHINA'

    def __init__(self,name,age):
        self.__name = name
        self.age = age

    def __play(self):
        print("正在学习")

def main():
    p = Person('张三',18)
    print(p.__country)

if __name__ == '__main__':
    main()

运行结果:

Traceback (most recent call last):
  File "C:/Users/10270/Desktop/py_test/test_10_6.py", line 57, in 
    main()
  File "C:/Users/10270/Desktop/py_test/test_10_6.py", line 54, in main
    print(p.__country)
AttributeError: 'Person' object has no attribute '__country'

如果想使用的话,可以通过如下的方式:

print(dir(p)) #加一行这个,可以看到私有属性被python内部进行了包装.

结果:

['_Person__country', '_Person__name', '_Person__play', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age']

其中的’_Person__country’, ‘_Person__name’, ‘_Person__play’, 前面被加上了_Person,所以在类外部是通过类名._Person__country,可以访问.

class Person(object):
    __country = 'CHINA'

    def __init__(self,name,age):
        self.__name = name
        self.age = age

    def __play(self):
        print("正在学习")

def main():
    p = Person('张三',18)
    print(dir(p))
    print(Person._Person__country)
    print(p._Person__country,p._Person__name,p.age)

运行结果:

['_Person__country', '_Person__name', '_Person__play', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age']
CHINA
CHINA 张三 18

私有方法也是同样的调用方法.
以上访问方式在实际应用中不推荐.建议使用下面的方式.
二.正常的访问方式
通过在类中定义方法来访问.

    class Person(object):
    __country = 'CHINA'

    def __init__(self,name,age):
        self.__name = name
        self.age = age

    def get_name(self): #通过自定义get_属性名()方法来操作私有属性,然后在类外部,直接用对象来访问
        return self.__name
    @classmethod
    def get_country(cls):#通过自定义get_属性名()方法来操作私有类属性,然后在类外部,直接用对象或者类来访问
        return cls.__country
    def get_play(self):#通过自定义get_方法名()方法来操作私有方法,然后在类外部,直接用对象来访问
        self.__play()
    def __play(self):
        print("正在学习")

def main():
    p = Person('张三',18)
    print(p.get_name())#使用对象来调用
    print(Person.get_country())#使用类来调用
    print(p.get_country())#使用对象来调用
    p.get_play()#使用对象来调用

if __name__ == '__main__':
    main()

运行结果:

张三
CHINA
CHINA
正在学习

3.私有属性的修改
如果你非要去类名.__属性名,或者对象.__属性名去使用,那么就相当于新增一个属性.

class Person(object):
    __country = 'CHINA'

    def __init__(self,name,age):
        self.__name = name
        self.age = age

    def get_name(self):
        return self.__name
    @classmethod
    def get_country(cls):
        return cls.__country
    def get_play(self):
        self.__play()
    def __play(self):
        print("正在学习")
 def main():
 	Person.__country = 'JAPAN'
    print(Person.__country)
    print(dir(Person))
    p.__country = 'AMERICA'
    print(p.__country)
    print(dir(p))
if __name__ == '__main__':
    main()

运行结果:

APAN
['_Person__country', '_Person__play', '__class__', '__country', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'get_country', 'get_name', 'get_play']  
AMERICA
['_Person__country', '_Person__name', '_Person__play', '__class__', '__country', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'get_country', 'get_name', 'get_play']

可以看到类和对象的名称空间中均增加了__country,且原有的_Person__country还在.
如果想修改_Person__country这个,则直接使用类名._Person__country或者对象名._Person__country进行修改,但是不推荐使用这种方法.
下面给出推荐的修改私有属性的方法:

class Person(object):
    __country = 'CHINA'

    def __init__(self,name,age):
        self.__name = name
        self.age = age

    @classmethod
    def set_country(cls,country):
        cls.__country = country

    def set_name(self,name):
        self.__name = name

    def get_name(self):
        return self.__name

    @classmethod
    def get_country(cls):
        return cls.__country

运行结果:

def main():
    p = Person('张三',18)
    print(p.get_name())
    print(Person.get_country())
    p.set_name('李四')
    Person.set_country('JAPAN')
    print(p.get_name())
    print(Person.get_country())

if __name__ == '__main__':
    main()

这里面有个类变量的坑:反正我自己一开始是弄错了的.
如下代码:

class Person(object):
    country = 'CHINA'

    def __init__(self,name,age):
        self.name = name
        self.age = age

def main():
    p = Person('张三',18)
    print(p.__dict__)
    print(Person.__dict__)
    print(p.country,Person.country)
    p.country = 'JAPAN'
    print(p.__dict__)
    print(Person.__dict__)
    print(p.country, Person.country)
    
if __name__ == '__main__':
    main()

运行结果:

{'name': '张三', 'age': 18}
{'__module__': '__main__', 'country': 'CHINA', '__init__': , '__dict__': , '__weakref__': , '__doc__': None}
CHINA CHINA
{'name': '张三', 'age': 18, 'country': 'JAPAN'}
{'__module__': '__main__', 'country': 'CHINA', '__init__': , '__dict__': , '__weakref__': , '__doc__': None}
JAPAN CHIN

可以看到一开始使用类名和对象名去访问类变量时,它们的值是一样的,后面通过对象尝试去修改country的值,我以为修改之后,类名访问的country的值也会改变,实际并不是,原因就是类变量是属于类的,对象如果自己在dict字典中没有的话,可以使用这个值,但是不能修改,如果想要 p.country = 'JAPAN’这样去修改,python则会去自己的dict字典中修改country,发现没有,就会增加这个同名的变量.所以它们在内存中分别是两个变量.
如果没有执行这条语句,那么对象去访问的就是类的变量,通过类名去修改类变量的值,对象去访问这个变量,那肯定也是被修改的.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/295893.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号