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 Show
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 example:
hoạt động tốt
không hoạt động ... dẫn đến lỗi loại
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 raC:/Users/TutorialsPoint1/~.py class foo: def bar(): print 'Hello'
Cập nhật vào ngày 13 tháng 8 năm 2020 05:54:06
6def far(n): factorial = 1 if int(n) >= 1: for i in range (1, int(n)+1): factorial = factorial * i return factorial1 9 def far(n): factorial = 1 if int(n) >= 1: for i in range (1, int(n)+1): factorial = factorial * i return factorial3 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 factorial5inspect 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) 4 class DataFrame(NDFrame): """ Two-dimensional size-mutable, potentially heterogeneous tabula3Tô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 2 3Vì 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? 1 2Output: 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 2 3 4 6 6 8 9 0 6 2 3 4 9 0 7 8 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 8 9 8 9 #baz.py import inspect class foo: def bar(): print 'Hello' print(inspect.getsource(foo))6 6 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 9 C:/Users/TutorialsPoint1/~.py class foo: def bar(): print 'Hello'2 1C:/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 2 3 4 6 6 8 9 0 1def 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 8 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 8 9 8 9 #baz.py import inspect class foo: def bar(): print 'Hello' print(inspect.getsource(foo))6 6 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 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 factorial9 Output: 0import inspect inspect.getsource(factorial) 2 C:/Users/TutorialsPoint1/~.py class foo: def bar(): print 'Hello' 8C:/Users/TutorialsPoint1/~.py class foo: def bar(): print 'Hello'9 9 def test(x): return (x+2)*(x-2)1 Python3Sử 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 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; 03 9 05 1 07 08def test(x): return (x+2)*(x-2)5 Output: 1Tô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. |