Python (4)

Source: Internet
Author: User

First, the decoration device

The adorner is to wrap the function, add some additional functions to the function, the adorner is a function, the parameter is the function that is wrapped

For example, add a function to the function, only need to add the adorner, and then other functions to refer to OK

(1) Define adorners but no references

Code: DEF outer: #定义装饰器 def wrapper (): Fun () print ' Yanzheng ' return wrapper@outer #链接上装饰器def func1 (): print ' func1 ' func1 () results: func1

(2) defining adorners and referencing

Code: DEF outer (fun): #定义装饰器 def wrapper (): print ' new feature 1 ' #添加新功能1, written on fun () is on top fun ()  print ' new feature 2 ' #添加新功能2, written under fun () is below print ' Yanzheng ' return wrapper@outer #链接上装饰器def Func1 (): print ' func1 ' func1 () Results: New features 1FUNC1 new features 2

(3) If there are other parameters in the function

Code: DEF outer (fun): Def wrapper (ARG): #如果被装饰的函数中有其他参数的话就在这里加上 print ' Validate ' fun (ARG) #如果被装饰的 If there are other parameters in the function, add the print ' Yanzheng ' return wrapper@outerdef func1 (ARG): #这里有参数 print ' func1 ', Argfunc 1 (' hello ') Result: Verify Func1 Helloyanzheng

Second, object-oriented

Object-oriented including: encapsulation, inheritance, polymorphism

Object-oriented Terminology: classes, class objects, instance objects, properties, functions, and methods

(1) Classes and objects

Code:class province:                           #叫做类     memo =    ' One of the 23 provinces of China '              #叫做静态字段, obtained through class, belongs to the class     def __init__ (Self,name,shenghui):    #叫做类的方法, SEFL is a must have the same, behind the dispensable         self. name = name                  #叫做动态字段, the         self obtained with self. Shenghui = shenghui a = province (' Hebei ', ' Tangshan ')               #河北对应name, Tangshan corresponds to Shenghui, which is equivalent to instantiating an object A, and can then read the property by a print a.name                     & NBsp;       #通过a来读取属性         print a. Shenghuiprint province.memo Result: One of the 23 provinces of Tangshan, Hebei, China





















This article is from the "Ming python" blog, so be sure to keep this source http://pythonzhai.blog.51cto.com/10391994/1728878

Python (4)

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.