Usage Analysis of the callable object _ call _ method in Python

Source: Internet
Author: User

Foreword recently, many friends asked me about the usefulness of Python callable objects. Why is it necessary to overload brackets rather than directly bind common methods of classes. Next, let's share with you some ideas about the callable object of _ call. Simplified Code to facilitate interface calling by using [python] class route (object): def _ init _ (self, res) self. resource = res @ classmethod def factory (cls): print 'factory 'return cls () @ webob. dec. wsgify def _ call _ (self, req): print 'route _ call _ 'return self. resource () class resource (object): @ webob. dec. wsgify def _ call _ (self, req): print 'Resource _ call _ 'class API (route): def _ init _ (self ): res = resource () super (API, sel F ). _ init _ (res) wsgi. server (eventlet. listen ('', 80), API. the code above factory () is an excerpt from a typical WSGI service. If _ call __is not required, we may need to specify or standardize an interface between each component. For example, below, everyone is called notcall ()... [Python] class route (object): def _ init _ (self, res) self. resource = res @ classmethod def factory (cls): print 'factory 'return cls () @ webob. dec. wsgify def notcall (self, req): print 'route notcall' return self. resource. notcall () class resource (object): @ webob. dec. wsgify def notcall (self, req): print 'Resource notcall' class API (route): def _ init _ (self): res = resource () super (API, self ). _ init _ (re S) wsgi. server (eventlet. listen ('', 80), API. factory (). in this way, notcall () is very difficult to use. Inter-module cooperation requires that you specify the Interface Name, write many interface documents, increase the amount of code and be prone to errors. You only want a function, but can do more than just function work like the above Code. The interface parameters of many modules require a function call, such as wsgi. server (port, app), the second parameter is a function call of the actual wsgi service. Today, when OOP is widely used, it seems that everything on the earth and in the universe can be abstracted as objects. However, in actual coding, do we really need to abstract everything into objects? This is one of the reasons why I like Python. Although everything in Python is an object, it provides such an object callable method, which can do some work that functions cannot do. For example, static variables are not allowed in Python, but you can do this using _ call _ [python] class Factorial: def _ init _ (self ): self. cache = {} def _ call _ (self, n): if n not in self. cache: if n = 0: self. cache [n] = 1 else: self. cache [n] = n * self. _ call _ (n-1) return self. cache [n] fact = Factorial () for I in xrange (10): print ("{}! = {} ". Format (I, fact (I) # output 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 when binding an object that involves binding a new class object, the operation code [python] class test (type): pass class test1 (test) can be used to place the object binding in the Meta class ): def _ call _ (self): print "I am in call" class test2 (object): _ metaclass __= test1 t = test2 () # I am in call test2 is an instance of test1. Because test1 is a metadata class. When the instance is bound to a metaclass, __call _ will call

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.