Python uses inspect to view code parameters

Source: Internet
Author: User

Use import inspect to view the parameters and modules, function code of the Python class

The file is the smallest module, the folder is the larger module.

The file can contain classes, functions.

A function can perform an operation where multiple functions can be written as a module, written in a class based on different things, which contains several functions written in several classes, and can be used as a file.

The main step is to set the file path to the system, introduce the file as a module, and start looking at the contents of the file.

First, a function was written

 def   H ():  print   " hello   " def   HM (M,K):  print   M, K  class      W (object):  def  __init__    (A, self): name  =a  def   g (self):  print  Name, "Hello world!   

Save in Path \ c under, a name called hello.py

Open the Python Shell window and add the path to the system path. The command is as follows

Import Sys

Sys.path.append (' c:/')

Introduce the file as a module.

Import Hello

Import Inspect

View the entire module Hello source code: Inspect.getsource (Hello) The whole look is not good, need print inspect.getsource (hello)

View module Hello inside wo All code for this class print Inspect.getsource (HELLO.W)

To view the code for a function within a module: print Inspect.getsource (hello.h)

To view the code for a function in a class within a module print Inspect.getsource (HELLO.W.G)

Code to view the parameters of a function in a module: Inspect.getargspec (HELLO.HM)

View the parameter code inspect.getargspec (hello.w.__init__) #这里还是查看类的初始定义函数 of the class in the module.

View the function parameter code inspect.getargspec (HELLO.W.G) in the class

View module path Inspect.getabsfile (hello)

View the path of a class in a folder module Inspect.getabsfile (... ) #结果是显示类的初始定义函数__init__ the location of the. Py.

>>> inspect.getabsfile (django.db) ' C:\\python27\\lib\\site-packages\\django-1.7.1-py2.7.egg\\django\\db \\__init__.py '

These should be enough for study. What can be added later.

Python uses inspect to view code parameters

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.