python中私有属性的理解

本文详细介绍了Python中面向对象类模板中的私有属性如何访问与修改,提倡正确使用getters和setters进行封装,避免直接操作私有变量。同时揭示了类变量和实例变量的区别,以及如何在实际开发中避免潜在陷阱。
摘要由CSDN通过智能技术生成

面向对象的类模板中有内置属性,私有属性,固定属性,普通属性
内置属性:类中以__开头和以__结尾的变量或者方法都是内置属性
私有属性:类中以__开头的变量或者方法都是私有属性
固定属性:__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 <module>
    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()

运行结果:

JAPAN
['_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__': <function Person.__init__ at 0x0000027B87361C80>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
CHINA CHINA
{'name': '张三', 'age': 18, 'country': 'JAPAN'}
{'__module__': '__main__', 'country': 'CHINA', '__init__': <function Person.__init__ at 0x0000027B87361C80>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
JAPAN CHIN

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

如梦@_@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值