[Python dishes] _ call _ What is

Source: Internet
Author: User
The python2.73python function has a function called closure. In fact, there is also a type of operation similar to the closure effect.
>>> Class :... pass...> A = A >>> A <class _ main __. a At 0x02bec500 >>>> B = a () >>> B <__ main __. A instance at 0x02c1bbe8 >>> type (a) <type 'classobj '>#a is a Class Object >>> type (B) <type 'instance'> # B is a class instance> A () <__ main __. A instance at 0x02c15800> # An object is instantiated with brackets> B () traceback (most recent call last): file "<stdin>", line 1, in <module> attributeerror: A instance has no _ call _ method # An _ call _ occurs when an object is called __
??? What is this call? Generally, an instance can only call methods or attributes. the form of B A () is not correct, but python can make class instances can also be directly called using similar methods. See the explanation class instances are described below in the following document. class instances are callable only when the class has a _ call _ () method; X (arguments) is a shorthand for X. _ call _ (arguments) when a class implements the _ call _ method, it can be called.
>>> Class B :... def _ call _ (Self ):... print 'Hi'... >>> B () <__ main __. B instance at 0x02c15e18 >>> B () # generate an instance and call its own Hi >>> B = B () >>> B () Hi
However, it is hard to understand what is delicious. You found that many people in stackoverflow also asked this question. Look for other people's opinions. In the blog of Bruce Eckel, author of thinking in various languages, can you find such a point to look at http://www.artima.com/weblogs/viewpost.jsp? Thread = 240845 refers to the use of _ call _ class as a non-parameter modifier.
Class decoratorwithoutarguments (object): def _ init _ (self, f): "if there are no decorator arguments, the function to be decorated is passed to the constructor. "print" Inside _ init _ () "self. F = f def _ call _ (self, * ARGs): "the _ call _ method is not called until the decorated function is called. "print" Inside _ call _ () "self. F (* ARGs) print "after self. F (* ARGs) "@ decoratorwithoutargumentsdef sayhello (A1, A2, A3, A4): Print 'sayhello arguments: ', A1, A2, A3, a4print "after decoration" # This type of decorator will be called once in _ init _ and every call will call _ print "Preparing to call sayhello () "sayhello (" say "," hello "," argument "," list ") print" after first sayhello () call "sayhello (" A "," different ", "Set of", "arguments") print "after second sayhello () call"
 

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.