The power of Python introspection

Source: Internet
Author: User
Introspection: code can view other modules and functions in the form of objects in the memory, obtain their information, and operate on them. In this method, you can define a function without a name, call a function without the declared parameter order, or even reference a function without a name in advance. & #160; Python allows function parameters to have default values. if a parameter is not applicable when a function is called, the parameter obtains its default value. You can also use named parameters

Introspection: code can view other modules and functions in the form of objects in the memory, obtain their information, and operate on them.

In this method, you can define a function without a name, call a function without the declared parameter order, or even reference a function without a name in advance.

 

Python allows a function parameter to have a default value. if a parameter is not applicable when a function is called, the parameter obtains its default value. You can also specify parameters in any order by using named parameters. (A parameter with a default value is an optional parameter .)

 

Some built-in functions in Python:

  1. Type function

The type function can receive anything as a parameter and return its data type.

  1. Str function

The str function forcibly converts data to a string. Each data type can be forcibly converted to a string.

You can use the str function to act on the module. the returned path is the module path name.

  1. Dir function

The dir function returns the attribute and method list of any object, including the module object, function object, String object, list object, and dictionary object.

  1. Callable function

The callable function receives any object as a parameter. if the parameter object is adjustable, True is returned; otherwise, False is returned.

 

General built-in functions are classified into the special module _ builtin.

  1. Getattr function

The getattr function returns a reference to a function that is not named until running. (Function reference !)

The getattr function also supports the default value.

 

Filter list in Python:

[Mapping-expressionfor element in source-list if filter-expression]

The first part is list resolution, and the last part is the filter expression starting with if. The filter can be any expression that returns true or false.

 

Special properties of and or in Python:

In Python, and or perform Boolean logic operations. But they do not return boolean values, but return one of the values actually compared.

The boolean environment is executed from left to right, and the return result is the first one without comparison, but the boolean value can be determined, one of the actual values !!

And or can constitute invalid scenarios in many cases.

 

Lambda functions in Python:

Python supports this cool syntax, allowing you to quickly define the smallest function of a single row.

 

After understanding the above concepts, you need to understand the following code:


def info(object,spacing=10,collapse=1):  '''Print methods and doc strings.   Takes module, class, list, dictionary, or string. '''   methodlist=[method for method in dir(object) if callable(getattr(object,method))]  processFunc=collapse and (lambda s: "".join(s.split())) or (lambda s: s)  print "\n".join(["%s%s"%        (method.ljust(spacing),        processFunc(str(getattr(object,method)._doc_)))        for method in methodlist        ])        if __name__ == "__main__":    print info.__doc__


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.