Python __call__ function and usage of built-in functions

Source: Internet
Author: User

The school began to enter the laboratory, need to assist big bro work, mainly in OpenStack code parsing, but involves a lot of Python advanced usage, a bit of trouble at the same time, while doing the project slowly update the blog. This time first write about the use of __call__, because often see it but do not know exactly what to do.

Discovering that __call__ actually overloads a class with a "()", which means that a class can be called like a function, saying it might not be clear, directly on the example.

#/usr/bin/env pythonclassTest:def __init__(self,a): SELF.A=adef __call__(self,b): C= self.a+bPrintCdefdisplay (self):Printself.atest= Test ("This is test!") Test.display () Test ("# #Append Something")

Output result: This is test!

This is test!# #Append something

As you can see, the __init__ function initializes the value in self as if it were a constructor, assigns a to "This is test!", and then we invoke the instance name directly, and here is the role of __call__ ().

As for why to do so, let me slowly discover it.


Python __call__ function and usage of built-in functions

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: 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.