Python Learning Chapter 22nd

Source: Internet
Author: User
Tags reflection

Today's content:

1. Properties

2. Class methods

3. Static methods

4. Reflection

1. Properties

refers to the invocation of a function in a class as if it were a call to an object property value. More Authoritative Solutions Interpretation: masquerading a method as a property, although at the level of the code there is no advanced, but make it look more reasonable.

Example:

 class   A:  def  __init__  (Self,*args): Self.name  = Args[0] Self.heig HT  = Args[1] self.weight  = Args[2] @prop Erty  def   BMI (self):  return  self.height/self.weight**2a1  = A ("  leon   ", 1.72,60< Span style= "COLOR: #000000" >)  print  (A1.BMI) 

Note that this is done by adding @property this adorner,

is the attribute, then can also be modified, about the modification and deletion, is implemented by the following method,

Example:

classB:def __init__(self,*args): Self.name=args[0] self.__pwd= Args[1] @propertydefpwd (self):returnSelf.__pwd@pwd. Setter#Notice that the adorner name is the same as the function name above, and the following function is consistent, and the following assignment will be passed into the new_pwd .    defpwd (self,new_pwd): Self.__pwd=new_pwdobj1= B ("Leon","hao123") Obj1.pwd= 123456Print(OBJ1.PWD)

Example of a delete operation for a property:

classB:def __init__(self,*args): Self.name=args[0] self.__pwd= Args[1] @propertydefpwd (self):returnSelf.__pwd@pwd. Setter#Notice that the adorner name is the same as the function name above, and the following function is consistent, and the following assignment will be passed into the new_pwd .    defpwd (self,new_pwd): Self.__pwd=new_pwd @pwd. Deleterdefpwd (self):delSelf.__pwdobj1= B ("Leon","hao123") Obj1.pwd= 123456Print(OBJ1.PWD)delobj1.pwdPrint(OBJ1.PWD)

The following procedures are described:

# The first step is to change the properties to automatically perform the method that is decorated by this @pwd.setter decorator
# Step two, pass the new value to the method that is decorated by the @pwd.setter adorner, as a parameter.

2. Class method, static method

Class method: Class name to invoke the class method, automatically passing the class's space to the CLS in the class method. object automatically passes class space to the CLS in the class if the class method is called

Example:

class C:     = 1    def__init__(self,*args):        = args[0]        self.age  = args[1 ]    @classmethod    def  Class_fun (CLS):        print= C ("Leon",) c1.class_fun ()

What is the use of class methods?
It is not necessary to create an object at the time of operation, using the class method when the class is directly allowed to manipulate the method in the class.

3. Static methods

Static method: Define a static method in the class, without passing in your class space, object space, can be used as a normal function. (That is, the independent function in the class that does not have to pass in to self again)

Example:

class D:     def __init__ (self):         Pass     @staticmethod    def fun  (A, b        ):return A += D () Print (D1.fun (2,3))

4. Reflection (very, very important,)

Reflection: Manipulating a space (object) through a string.

There are a total of four methods:

1.getattr ()

2.hasattr ()

3.setattr ()

4.delattr ()

Different ways to use the four scenarios:

1, instantiate an object to study

   

Python Learning Chapter 22nd

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.