class in such an easy manner as to check parameters and use similar properties? For the perfect Python programmer, this must be done!Remember the adorner (decorator) can add functionality to a function dynamically? For class methods, adorners work as well. Python's built-in @prop
same, equivalent.
Built-in 3 decorators, they are staticmethod,classmethod,property, their role is to change the method defined in the class into static methods, class methods and properties, as follows:
Copy the Code code as follows:
Class Rabbit (object):
def __init__ (self, name):
Self._name = Name
@staticmethod
@property can access a Python-defined function as a property, providing a more friendly way to access it, but sometimes setter/getter is needed.Suppose a class CLS is defined, which must inherit from the object class, with a private variable __x1. The first method of using a
=='Master': ....: Self.__sit_down () in [7]: w=Dog () in [8]: W.sit_down ('Master') sat down in [9]: W.sit_down ('Master') in [Ten]: W.__sit_down ()---------------------------------------------------------------------------Attributeerror Traceback (most recent)Ten-16df8360e9bf>inch()---->1W.__sit_down () Attributeerror:'Dog' Objecthas no attribute'__sit_down'In [ -]: W._dog__sit_down () sit down.Python instances can invoke the public method of Python
The decorator contains a lot of advanced Python skills, and @ property is no exception. for example, an example of rapid code refactoring will be mentioned later, here we will illustrate the usage of @ property decorators in Python programming with examples:
Value and value
, ' evalute ' is only output once. If you look at my previous posts, the mechanism for @lazy should be well understood.Here, the lazy class has the __get__ method, the description is a descriptor, the first time to execute C.area, because the order problem, first go to c.__dict__, did not find, go to class space to find, in the class circle, there is area () meth
The usage and meaning of python @ property are fully parsed, and @ property is fully parsed.
The code that you first came into contact with python is to take the length and width of the rectangle, define a rectangle class, set the length and width attributes of the rectangle
Property () and pythonproperty of Python decorators
1. What is a decoration device?
Official definition: the decorator is a well-known design model that is often used in scenarios with cut-plane requirements. It is more classic in terms of log insertion, performance testing, and transaction processing. The decorator is
easier. Here are some of the changes she would probably make:
# The second version of the Money Class class money: def __init__ (self, dollars, cents): self.total_cents = dollars * + cents
This change has a consequence: every line of code that references the Money class must be adjusted. Sometimes it's lucky that you're the maintainer of all this code, and
The property in Python is more useful.First, a code.#-*-coding:utf-8-*-class C (object): Status_dict = {1: ' Accept ', 2: ' Reject '}def __init__ (self): self._x = 1@propertydef St ATUs (self): return self.status_dict[self._x] @status. Setterdef status (Self, val): if val = = ' Reject ': self._x = 2elif val = = ' Accept ': self._x = 1c = C () print c.statusprint
The following comes from Python 3.6.0 Document:
class property (fget=none, fset=none, fdel=none, doc=none)
Return a property attribute.
Fget is a function for getting an attribute value. Fset is a function for setting an attribute value. Fdel is a function for d
class in such an easy manner as to check parameters and use similar properties? For the perfect Python programmer, this must be done!Remember the adorner (decorator) can add functionality to a function dynamically? For class methods, adorners work as well. Python's built-in @prope
such an easy manner as to check parameters and use similar properties? For the perfect Python programmer, this must be done!
Remember the adorner (decorator) can add functionality to a function dynamically? For class methods, adorners work as well. Python's built-in @property deco
Can the adorner (decorator) dynamically add functionality to the function? For class methods, adorners work as well. Python built-in@property Decorator is responsible for turning a method into a property invocation:Class Student (
(value, Decimal): -Self._fee =value to + #---------------------------------------------------------------------- - if __name__=="__main__": thef =Fees () * $ #The code above demonstrates how to create a setter method for the fee property. You can use a # decorator called @fee.setter to decorate the second method name is also the fee method to implement this. The setter is called when you do the followin
so, Python creates a class object named trick in memory by __metaclass__, which is trick. If Python does not find __metaclass__, it will continue to look for the __metaclass__ property in its parent class Flytosky, and attempt to create a trick
:
?
1 2 3 4 5 6 7 8
>>> S = Student () >>> S. set_score (60) # OK! >>> S. get_score () 60 >>> S. set_score (9999) Traceback (most recent call last ): ... ValueError: score must between 0 ~ 100!
However, the above call method is a little complicated, and it is not as simple as directly using attributes.
Is there a simple way to check parameters and use variables similar to attributes? For a Python programmer who pursues pe
Python built-in functions (51) -- property, pythonproperty
English document:
Classproperty(Fget = None,Fset = None,Fdel = None,Doc = None)
Return a property attribute.
FgetIs a function for getting an attribute value.FsetIs a function for setting an attribute value.FdelIs a function for deleting an attribute value. AndDocCreates a docstring for the attribute.
If
Python has a small concept called a property function that can do something useful. In this article, we will see how to do the following:
To convert a class method to a read-only property
Re-implement a property setter and getter method
In this article, you
; S.get_score >>> S.set_score (9999) Traceback (most recent call last): ... Valueerror:score must between 0 ~ 100!
However, the above method of invocation is slightly more complex, not directly with the attributes so straightforward.
Are there any variables that can be used to check the parameters and to access the class in a simple way such as a property? For the perfect
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.