Private attributes are attributes starting with **double underscore'__'**.
Accessing private properties from outside will throw an exception, indicating that there is no such property.
Although private properties cannot be accessed from outside, they can be accessed from inside the class. Private attributes are designed to protect class or instance attributes from external pollution.
For more private attributes to read and modify, please refer to add link description
Operate the private properties of the instance object
lass Person(object):
def __init__(self, name, sex, age):
self.__name = name
self.__sex = sex
self.__age = age
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
def get_sex(self):
return self.__sex
def set_sex(self, sex):
self.__sex = sex
def get_age(self):
return self.__age
def set_age(self, age):
self.__age = age
def get_info(self):
print(self.get_name(), self.get_sex(), self.get_age())
xiaoming = Person('xiaoming', 'M', 20)
xiaoming.set_age(18)
xiaoming.get_info()
输出-->xiaoming M 18
The method of the instance refers to the function defined in the class. The first parameter of the instance method is always self. Self is a reference to the instance object itself that calls the method. In addition, other parameters and ordinary functions are completely the same.
When calling an instance method externally, there is no need to explicitly pass the self parameter. In addition, this method of operating private attributes by defining instance methods is recommended. In addition to protecting internal data consistency, this form of data encapsulation can also simplify the difficulty of external calls. Of course, instance methods are not just for private properties. We can abstract operations related to class instances into instance methods, such as printing detailed information about the instance, and so on.
In order to manipulate the private properties of the instance object, we define the instance method; similarly, if you need to manipulate the private properties of the class, you should define the method of the class.
Private properties of the operation class
class Person(object):
__count = 0
def __init__(self, name, sex, age):
self.name = name
self.sex = sex
self.age = age
Person.__count += 1
print("%i" % (Person.__count))
'''@classmethod:类方法的标志'''
@classmethod
def getCount(cls):
return cls.__count
@classmethod
def setCount(cls, count):
cls.__count = count
def get_count(self):
return self.__count
def set_count(self, count):
self.__count = count
xiaoming = Person('xiaoming', 'M', 18)
xin = Person('xin', 'F', 18)
print(xiaoming.name, xiaoming.sex, xiaoming.age)
print(xin.name, xin.sex, xin.age)
##下面这一行属于实例对象通过调用实例方法使用私有属性
xin.set_count(3)
print("the number of objects is %i" % (xin.get_count()))
print("the number of objects is %i" % (xiaoming.get_count()))
print("the number of objects is %i" % (xin.getCount()))
print("the number of objects is %i" % (xiaoming.getCount()))
'''注释的这一行执行报错,说明实例对象方法只能由实例对象调用,但是类方法既可以被类调用也可以被实例对象调用。'''
# print("the number of objects is %i" % (Person.get_count()))
print("the number of objects is %i" % (Person.getCount()))
print("类通过类方法使用私有属性")
Person.setCount(5)
print("the number of objects is %i" % (xin.get_count()))
print("the number of objects is %i" % (xiaoming.get_count()))
print("the number of objects is %i" % (xin.getCount()))
print("the number of objects is %i" % (xiaoming.getCount()))
print("the number of objects is %i" % (Person.getCount()))
Output:
It can be seen from the result that using the private properties of the class through the instance method has the same effect as using the private properties of the instance object through the instance method. If the instance object does not modify the class private property through the instance method, the value of the class private property obtained by the instance object is the value of the class private property obtained by the class
Unlike the instance method, there are two points that need special attention:
1. The class method needs to be marked as a class method with @classmethod, otherwise the definition is still an instance method
2. The first parameter of the class method will be passed into the class itself, usually the parameter name is named cls, the above cls.__localtion is actually Equivalent to Person.__localtion.
Because it is called on the class, not on the instance, the class method cannot get any instance variables, only the class reference