Python full stack day28 (Decorator for Class)

Source: Internet
Author: User

Class is an object and can be decorated like a function

Decorator for class. py

def deco (obj):    print (' ====== ', obj)    obj.x=1    obj.y=2    obj.z=3    return obj# @deco  #test =deco ( Test) # DEF Test (): #     print (' Test function run ')
#运行了装饰器所以打印了装饰器里面的print内容 @deco #Foo =deco (Foo)
#======class Foo: Pass
#打印类的字典 where the XYZ attribute is defined in the adorner function as print (foo.__dict__) #{' __doc__ ': None, ' Z ': 3, ' Y ': 2, ' __dict__ ': <attribute ' __dict__ ' of ' foo ' objects>, ' __weakref__ ': <attribute ' __weakref__ ' of ' foo ' objects>, ' x ': 1, ' __module__ ': ' __main__ '}

This decorator is to pass the class as an argument to the function Deco and then assign the return value to Foo, and in the adorner function you can define the properties of the corresponding class.

Python full stack day28 (Decorator for Class)

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.