Python 安全修改私有属性

设置私有属性之后,如何修改私有属性

class Room:
    def  __init__(self,name,length,width):
        self.__name = name
        self.__length = length
        self.__width = width

    def get_name(self):
        return self.__name

    def set_name(self,newName): #约束
        if type(newName) is str and newName.isdigit() == False:
            self.__name = newName  #这样写的好处是。不能让别人随意修改。
        #起到一个保护作用。
        else:
            print("姓名不合法")

    def area(self):
        return  self.__length * self.__width

room = Room("小明",2,4)
print(room.area())
room.set_name("2")
print(room.get_name())
python修改私有属性
#问题:父类的私有属性,能否被子类调用。
#结果:不能被调用。
class Foo:
    __key = "保险柜的钥匙"   #_Foo_key

class Foo2(Foo):
    print(Foo.__key)        #_Foo2_key
View Code
父类的私有属性,能不能被子类调用的。
原文地址:https://www.cnblogs.com/ken-yu/p/12172046.html