Features in Python attribute
attribute is a variable inside an object
The state of an object is described by its attributes, and the method of the object can change its properties
Attributes can be accessed directly from outside the object
Examples of features:
Class Person: name = ' Yoda ' # class in attribute def get_name (self): # Access attribute via accessor method return self.name def set_ Name (self, value): # change attribute by accessor method Self.name = value run: A1 = person () A1.name # directly from external access attribute Yodaa1.name = ' master '
# directly from the outside. By default, Python supports external manipulation of attributes, but it destroys the encapsulation principle of the class, which is not accessible from external objects, and should use private attributes.
Privatization features in Python
To make the feature or method inaccessible externally, simply precede the name with a double underline.
Example of feature privatization:
Class Secret: __name = ' Yoda ' # Plus double underline privatization feature def get_name (self): return self.__name def __secret ( Self): # privatisation method print "can ' t find" def access (self): return Self.__secret () # Accessor Access private method A2 = Secret ( ) A2.__name # no access to features! A2._secret__name # or can be accessed from an external feature yoda-python essentially does not fully support privatization, but changes the name
Using attributes in Python to access and set attributes
There are some object-oriented languages that support private features that cannot be accessed directly from the outside, and need to be written getter and setter methods to manipulate these features
Python does not require getter and Seter methods, because all of the features in Python are public, and if you are not comfortable with the attributes of directly accessing objects, you can write setter and getter methods for objects, but a better solution is to use the property
- Python hides the accessor method so that all features look the same, and this property defined by the accessor is called an attribute
examples of property use :
# The first method uses the attribute class Foo:def __init__ (self): Self.name = ' Yoda ' self. Work = ' master ' def Get_person (self): return self.name,self.work def set_person (self,value): Self.name,self.work = Value Person = property (Get_person,set_person) runs as follows: A3 = foo () A3.person (' Yoda ', ' m Aster ') A3.person = ' Skylaer ', ' Programer ' A3.person (' Skylaer ', ' Programer ') # The second method uses the attribute class Foo:def __init__ (self): Self.name = ' Yoda ' self.work = ' master ' @property def person: return self.name,self.work @p Erson.setter # If you do not specify the Stter property, you cannot set its value from outside the class, which is useful for read-only features def person (self,value): Self.name,se Lf.work = Value 
Property () The first parameter is the Getter method, and the second argument is the setter method
Property functions can be called with 0,1,2,3 or 4 parameters, if there are no parameters, the resulting properties are neither readable nor writable, the four parameters are called Fget,fset,fel,fdoc, if you want a property to be write-only, and there is a document string, can use them as a keyword parameter
Python features, attributes, and privatization