Python _ call __

Source: Internet
Author: User

The _ call _ about Python has such an explanation in the official documentation (http://docs.python.org/reference/datamodel.html? Highlight =__ call __# object. _ call __)

Object._ Call __(Self[,ARGs...])

Called when the instance is "called" as a function; if this method is defined,X (arg1, Arg2, ...)Is a shorthandX._ Call __(Arg1, Arg2, ...).

When an instance is called as a method, such as instance (arg1, args2 ,...), in fact, the instance is called. _ call _ (arg1, arg2 ,...)

First, let's look at the following:

In [5]: Class :...: def _ init _ (Self ):...: Print "_ init _ method "...: def _ call _ (Self ):...: Print "_ call _ method "...: In [6]: A = a () _ init _ methodin [7]: A () _ call _ Method

The following can be ignored ......

 
# Coding: UTF-8 "" demo for _ call _ http://docs.python.org/reference/datamodel.html? Highlight =__ call __# object. _ call _ when we use an instance of a class as a method, the _ call _ method "Class: def _ init _ (self, ARG): Print "init" self. arg = Arg def _ call _ (Self): Print "A" Print self. arg if _ name _ = "_ main _": c = a ("AAAAA") C () # at this time, C is just an instance ), we generally use C. methodname (), but now () is directly added after the instance, which is called as a method. At this time, C (arg1, arg2,...) will be called ,....) it is equivalent to C. _ call _ (self, arg1, arg2 ,...)

 

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.