Python has a property syntax that resembles the get set syntax for C #, which has the following two points:
Set the class method to read-only property;
Implements the Getter and setter method of the attribute;
These two points are highlighted below:
To set a
Property methodThe function of a property method is to turn a method into a static property by @property .Class Dog (object): def __init__ (self,name): self.name = name @property def Eat (self): print ("%s
This is obviously illogical. To limit the scope of the score, you can set the score by one method, and set_score() then get the score by one, get_score() so that in the set_score() method, you can check the parameters: class studentdef get_scorereturn self._score def set_scoreif not Isinstance (value, int): raise valueerror ( ' score must is an integer! ') if value 0 or value > 100: span class= "keyword" >
explicit explanation when defining the class (Code Snippet 2), the Parrot (Code Snippet 2) We define will not inherit the object
The object class provides exactly the @property functionality we need, and in the documentation we can find the following information:
New-style classAny class which inherits from Object. Th
This article describes how to use python's property syntax, which has some reference value. if you need it, you can refer to it. Python has a property syntax, which is similar to the get set syntax of C #. it has the following functions:
Set the class method to read-only;
Implement the getter and setter methods of att
The @ property annotator of Python is used to call a class's method into a class's property. then @ property creates another Annotator and assigns a value to the property using one method. After @ property is used in the
In reading the Python notes of Liao Xuefeng, read the @property characteristics, found a slight deficiency, because they are beginners, hope to benefit more beginners. What is the purpose of @property? Here's a step-by-step description.
define class student, with variable name name and Scoreclassstudent (object):
Python learns Day 9 property multi-inheritance Mixin, pythonmixin
When binding an attribute, If we expose the attribute directly, although it is easy to write, but we cannot check the parameter, so we can change the score as needed:
s = Student()s.score = 9999
To limit the score range, you can use the set_score () method to set the score and get_score () to obtain the score. In this way, in the set_score ()
The use of @property in PythonThe role of @property is to turn a getter method into a property, @xxx. The setter turns the setter method into a property assignment.#coding: Utf-8 class screen(object): @property def
1. Single Underline:
A single underline named variables (including classes, functions, generic variables) cannot be imported into another module through the From module import *.
testa.py
# testa.py
_foo (object):
Fun (self):
print (' Fun in Class _foo ')
_bar (a):
Print (a)
testb.py
*
a = _foo ()
_bar (' Hello World ')
C = _foobarTestb.py will complain:
Traceback (most recent call last):
defined
2
Python property and pythonproperty
Generally, when the category class and instance attributes are used, the stored correlation value will be returned, that is, directly dealing with the _ dict _ of the class (instance. If you want to standardize these access and value setting methods,
One method is data descriptor, and
Look at the contents of the Python community aunt's organization there is an article about Python memory optimization, using the __slots__. Then check it out and summarize it. It feels very useful.
When the Python class is instantiated, there is a __dict__ property with the
Python has a property syntax that resembles the get set syntax for C #, which has the following two points:
Set the class method to read-only property;
Implements the Getter and setter method of the attribute;
These two points are highlighted below:
To set a
@property can access a Python-defined function as a property, providing a more friendly way to access it, but sometimes setter/getter is needed, why is it necessary to use functions as attributes?First, in order to provide friendly access, the second is to check the security of the parametersIf I define a Class A, whos
Python standard library: built-in Function property (fget = None, fset = None, fdel = None, doc = None), fgetfset
This function is used to set attributes of class members. The fget parameter is used to obtain the attribute value of a class member. The fset parameter is used to set the attribute value of a
Python @ propertyWhen reading Liao Xuefeng's Python notes, I read the @ property feature and found that it is slightly insufficient. Because I am a beginner, I hope to benefit more beginners. @ Property: What is the purpose? The following is a step-by-step introduction.
Definition
If classes are defined in Java, we often use classes to encapsulate properties, such as the student class, with FirstName and LastName, and, in order to be able to access and modify this property, we also set the set and get methods, but in Python, We can use the @property method to turn a method into a
. name = 'jason 'class B (A): def _ init _ (self): self. age = '16' super (B, self ). _ init _ () #. _ init _ (self) # Another method that inherits constructor d = B ()3. Polymorphism
Python itself supports polymorphism, so it doesn't make sense to discuss polymorphism in Python object-oriented, so I will not talk about it hereIv.
", lambda Self, x:print (x)))>>> T.AA' Hello '>>> T.AA = 33and set on an object instance, there is no effect:>>> setattr (t, ' AA ', property (lambda self: "Hello", lambda Self, x:print (x)))>>> setattr (Something, ' AA ', None)>>> T.AA>>> T.AA = 3In addition, the property can be defined by annotations (Annotation)Class C (object):| @
python--Privatization and Property propertiesI. Privatization
XX: Public variables
_x: Single front underline, privatized property or method, from somemodule import * Prohibit import, class object and subclass can access
__XX: Double-front underline, avoid naming conflicts with attributes in subclasses
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.