Hướng dẫn python print source code of module - python in mã nguồn của mô-đun

Tôi muốn in mã nguồn của một phương thức tích hợp. Ví dụ: toán học là một mô-đun tích hợp của Python và tôi muốn in mã nguồn của trần nhàmath is a built-in module of python and i want to print the source code of ceil

Tôi biết cách in mã nguồn của một mô -đun tùy chỉnh bằng cách sử dụng expect.getSourceinspect.getsource

Cần trợ giúp Tôi đang cố gắng tạo một chương trình nơi tôi có thể gọi bất kỳ phương thức hoặc hàm tích hợp nào và nó sẽ chỉ hiển thị mã nguồn của hàm hoặc mô -đun đó. Python có hầu hết mọi thứ trong thư viện tích hợp, tôi muốn sử dụng các thư viện này
I am trying to create a programme where i can call any builtin methods or functions and it will display only the source code of that function or module.
Python has almost everything in builtin library, i want to use these libraries

example:

input: factorial
output:
        def factorial(n):
            if n == 0:        
                return 1      
            else:      
                return n * factorial(n-1)

hoạt động tốt

import inspect
inspect.getsource(factorial)

không hoạt động ... dẫn đến lỗi loại

import inspect
import math
print(inspect.getsource(math.ceil)
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object

cảm ơn trước :)


Chúng tôi sử dụng phương thức GetSource () của mô -đun kiểm tra để lấy mã nguồn của hàm.

inspect.getsource(object)

Trả về văn bản của mã nguồn cho một đối tượng. Đối số có thể là một mô -đun, lớp, phương thức, chức năng, tracback, khung hoặc đối tượng mã. Mã nguồn được trả về dưới dạng một chuỗi. Một ioerror được nâng lên nếu mã nguồn không thể được truy xuất.

Nếu hàm được biên dịch từ chuỗi, luồng hoặc nhập từ tệp được biên dịch, thì bạn không thể truy xuất mã nguồn của nó.

Chúng tôi nhập mô -đun kiểm tra và truy xuất mã nguồn cho tập lệnh đã cho như sau

Thí dụ

#baz.py
import inspect
class foo:
      def bar():
          print 'Hello'
print(inspect.getsource(foo))

Đầu ra

C:/Users/TutorialsPoint1/~.py
class foo:
      def bar():
          print 'Hello'
 

Hướng dẫn python print source code of module - python in mã nguồn của mô-đun

Cập nhật vào ngày 13 tháng 8 năm 2020 05:54:06

  • Câu hỏi và câu trả lời liên quan
  • Đối tượng mã Python
  • Truy cập mã nguồn HTML bằng Selenium Python.
  • Truy xuất giá trị thuộc tính có chọn lọc từ mảng các đối tượng trong JavaScript
  • Mã nguồn của ABAP báo cáo mà không hạn chế
  • Sắp xếp lại mã nguồn đã cho trong C ++
  • Giải mã thông báo nguồn từ mã dựa trên một số thuật toán trong JavaScript
  • Làm thế nào để truy xuất mã sản phẩm gói MSI bằng PowerShell?
  • Làm thế nào tôi có thể lấy một tệp python từ một tệp python khác?
  • Làm thế nào để tìm ra mã nguồn của một giao dịch trong SAP?
  • Làm thế nào để biểu diễn mã nguồn của một đối tượng với các mảng JavaScript?
  • Hiển thị mã nguồn của RFC trong hệ thống SAP
  • Cài đặt MySQL từ nguồn
  • Làm thế nào để lấy đường dẫn mô -đun Python?
  • Làm thế nào để truy cập các đối tượng Python trong các đối tượng trong Python?
  • Làm thế nào để tìm nạp mã nguồn của giao dịch Lưu ý trong SAP R/3?

import inspect
inspect.getsource(factorial)
6
def far(n):
    factorial = 1
    if int(n) >= 1:
        for i in range (1, int(n)+1):
            factorial = factorial * i
    return factorial
1
import inspect
import math
print(inspect.getsource(math.ceil)
9
def far(n):
    factorial = 1
    if int(n) >= 1:
        for i in range (1, int(n)+1):
            factorial = factorial * i
    return factorial
3
inspect.getsource(object)
8
def far(n):
    factorial = 1
    if int(n) >= 1:
        for i in range (1, int(n)+1):
            factorial = factorial * i
    return factorial
5inspect module, dill module and dis module built-in standard libraries in Python programming. They provide several useful functions to track information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. getsource() method is used to get the source code of Python objects.

import inspect
inspect.getsource(factorial)
6
import inspect
inspect.getsource(factorial)
7
def far(n):
    factorial = 1
    if int(n) >= 1:
        for i in range (1, int(n)+1):
            factorial = factorial * i
    return factorial
8
inspect.getsource(object)

Sử dụng mô -đun thì là để truy xuất mã nguồnThe object type parameter, of which we want the source code.

Ở đây chúng tôi đang sử dụng mô -đun Dill trong Python để tìm nạp mã nguồn của đối tượng được chỉ định.text of the source code for an object

import inspect
inspect.getsource(factorial)
2
class DataFrame(NDFrame):
    """
    Two-dimensional size-mutable, potentially heterogeneous tabula
1
An IOError is raised if the source code cannot be retrieved.

import inspect inspect.getsource(factorial) 4 class DataFrame(NDFrame): """ Two-dimensional size-mutable, potentially heterogeneous tabula3

Tôi có thể xem mã nguồn Python ở đâu?Here we are importing the inspect module first and then using the inspect.getsource() function to fetch the source code of the given object.

python3

import inspect
inspect.getsource(factorial)
2
import inspect
inspect.getsource(factorial)
3

Vì Python là nguồn mở, bạn có thể đọc mã nguồn. Để tìm hiểu tệp nào mà một mô -đun hoặc hàm cụ thể được triển khai, bạn thường có thể in thuộc tính __file__. Ngoài ra, bạn có thể sử dụng mô -đun kiểm tra, xem phần truy xuất mã nguồn trong tài liệu kiểm tra.

Mô -đun Python có thể chứa in không?

import inspect
import math
print(inspect.getsource(math.ceil)
1
import inspect
import math
print(inspect.getsource(math.ceil)
2

Output:

def test(x):

   return (x+2)*(x-2)

Ví dụ: Ở đây chúng tôi đang tìm nạp mã nguồn cho đối tượng đã cho.Here we are fetching the source code for the given object.

python3

import inspect
inspect.getsource(factorial)
2
import inspect
inspect.getsource(factorial)
3

import inspect
inspect.getsource(factorial)
4
import inspect
import math
print(inspect.getsource(math.ceil)
6

import inspect
inspect.getsource(factorial)
6
import inspect
import math
print(inspect.getsource(math.ceil)
8
import inspect
import math
print(inspect.getsource(math.ceil)
9
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
0

import inspect
inspect.getsource(factorial)
6
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
2
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
3
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
4
import inspect
import math
print(inspect.getsource(math.ceil)
9
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
0
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
7

TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
8
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
9
inspect.getsource(object)
0
inspect.getsource(object)
1
inspect.getsource(object)
2
inspect.getsource(object)
3__

#baz.py
import inspect
class foo:
      def bar():
          print 'Hello'
print(inspect.getsource(foo))
1
import inspect
import math
print(inspect.getsource(math.ceil)
8
import inspect
import math
print(inspect.getsource(math.ceil)
9
import inspect
import math
print(inspect.getsource(math.ceil)
8
import inspect
inspect.getsource(factorial)
9
#baz.py
import inspect
class foo:
      def bar():
          print 'Hello'
print(inspect.getsource(foo))
6

import inspect
inspect.getsource(factorial)
6
import inspect
inspect.getsource(factorial)
7
#baz.py
import inspect
class foo:
      def bar():
          print 'Hello'
print(inspect.getsource(foo))
9

C:/Users/TutorialsPoint1/~.py
class foo:
      def bar():
          print 'Hello'
 
0
import inspect
import math
print(inspect.getsource(math.ceil)
9
C:/Users/TutorialsPoint1/~.py
class foo:
      def bar():
          print 'Hello'
 
2

import inspect
import math
print(inspect.getsource(math.ceil)
1
C:/Users/TutorialsPoint1/~.py
class foo:
      def bar():
          print 'Hello'
 
4

Output:

def far(n):
    factorial = 1
    if int(n) >= 1:
        for i in range (1, int(n)+1):
            factorial = factorial * i
    return factorial

Ví dụ: Chúng tôi cũng có thể sử dụng kiểm tra trên các chức năng và đối tượng thư viện tích hợp. & NBSP;We can use inspect on built in library functions and objects also. 

python3

import inspect
inspect.getsource(factorial)
2
import inspect
inspect.getsource(factorial)
3

import inspect
inspect.getsource(factorial)
4
import inspect
import math
print(inspect.getsource(math.ceil)
6

import inspect
inspect.getsource(factorial)
6
import inspect
import math
print(inspect.getsource(math.ceil)
8
import inspect
import math
print(inspect.getsource(math.ceil)
9
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
0

import inspect
import math
print(inspect.getsource(math.ceil)
1
def test(x):

   return (x+2)*(x-2)
3
def test(x):

   return (x+2)*(x-2)
4
def test(x):

   return (x+2)*(x-2)
5

Output:

class DataFrame(NDFrame):
    """
    Two-dimensional size-mutable, potentially heterogeneous tabula

import inspect inspect.getsource(factorial) 6TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object 2 TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object 3TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object 4import inspect import math print(inspect.getsource(math.ceil) 9 TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object 0TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object 7

TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
8
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
9
inspect.getsource(object)
0
inspect.getsource(object)
1
inspect.getsource(object)
2
inspect.getsource(object)
3__

Python3

#baz.py
import inspect
class foo:
      def bar():
          print 'Hello'
print(inspect.getsource(foo))
1
import inspect
import math
print(inspect.getsource(math.ceil)
8
import inspect
import math
print(inspect.getsource(math.ceil)
9
import inspect
import math
print(inspect.getsource(math.ceil)
8
import inspect
inspect.getsource(factorial)
9
#baz.py
import inspect
class foo:
      def bar():
          print 'Hello'
print(inspect.getsource(foo))
6

import inspect
inspect.getsource(factorial)
6
import inspect
inspect.getsource(factorial)
7
#baz.py
import inspect
class foo:
      def bar():
          print 'Hello'
print(inspect.getsource(foo))
9

C:/Users/TutorialsPoint1/~.py
class foo:
      def bar():
          print 'Hello'
 
0
import inspect
import math
print(inspect.getsource(math.ceil)
9
C:/Users/TutorialsPoint1/~.py
class foo:
      def bar():
          print 'Hello'
 
2

Ví dụ: Chúng tôi cũng có thể sử dụng kiểm tra trên các chức năng và đối tượng thư viện tích hợp. & NBSP;

def far(n):
    factorial = 1
    if int(n) >= 1:
        for i in range (1, int(n)+1):
            factorial = factorial * i
    return factorial
9

Output:

import inspect
inspect.getsource(factorial)
0

import inspect inspect.getsource(factorial) 2 C:/Users/TutorialsPoint1/~.py class foo:       def bar():           print 'Hello'  8

C:/Users/TutorialsPoint1/~.py
class foo:
      def bar():
          print 'Hello'
 
9
import inspect
import math
print(inspect.getsource(math.ceil)
9
def test(x):

   return (x+2)*(x-2)
1

Python3

Sử dụng mô -đun DIS để truy xuất mã nguồn

Ở đây chúng tôi đang sử dụng mô -đun DIS trong Python để tìm nạp mã nguồn của đối tượng được chỉ định.

C:/Users/TutorialsPoint1/~.py
class foo:
      def bar():
          print 'Hello'
 
0
import inspect
import math
print(inspect.getsource(math.ceil)
9
C:/Users/TutorialsPoint1/~.py
class foo:
      def bar():
          print 'Hello'
 
2

Ví dụ: Chúng tôi cũng có thể sử dụng kiểm tra trên các chức năng và đối tượng thư viện tích hợp. & NBSP;

import inspect
inspect.getsource(factorial)
03
import inspect
import math
print(inspect.getsource(math.ceil)
9
import inspect
inspect.getsource(factorial)
05

import inspect
import math
print(inspect.getsource(math.ceil)
1
import inspect
inspect.getsource(factorial)
07
import inspect
inspect.getsource(factorial)
08
def test(x):

   return (x+2)*(x-2)
5

Output:

import inspect
inspect.getsource(factorial)
1

Tôi có thể xem mã nguồn Python ở đâu?

Vì Python là nguồn mở, bạn có thể đọc mã nguồn.Để tìm hiểu tệp nào mà một mô -đun hoặc hàm cụ thể được triển khai, bạn thường có thể in thuộc tính __file__.Ngoài ra, bạn có thể sử dụng mô -đun kiểm tra, xem phần truy xuất mã nguồn trong tài liệu kiểm tra.print the __file__ attribute. Alternatively, you may use the inspect module, see the section Retrieving Source Code in the documentation of inspect .

Mô -đun Python có thể chứa in không?

Có. Chúng là các chức năng tích hợp (hoặc trong trường hợp danh sách, một lớp tích hợp). They're built-in functions (or in the case of list , a built-in class).

Python có mã nguồn không?

Bởi vì các ứng dụng Python thường được phân phối dưới dạng mã nguồn, vai trò của thời gian chạy Python là chuyển đổi mã nguồn Python và thực thi nó trong một bước.Trong nội bộ, thời gian chạy Cpython không biên dịch mã của bạn.Một quan niệm sai lầm phổ biến là Python là một ngôn ngữ được giải thích.Nó thực sự được biên soạn.Python applications are typically distributed as source code, the role of the Python runtime is to convert the Python source code and execute it in one step. Internally, the CPython runtime does compile your code. A popular misconception is that Python is an interpreted language. It is actually compiled.