Hướng dẫn python print source code - mã nguồn in python

Tôi đang cố gắng để có được mã nguồn in python bằng cách sử dụng kiểm tra nhưng đó là lỗi. Ai đó có thể vui lòng cho tôi biết cách lấy mã nguồn của câu lệnh in Python2.7.

Nội dung chính ShowShow

  • Làm thế nào để bạn in mã nguồn của một hàm trong Python?
  • Làm cách nào để in mã lớp trong Python?
  • Bạn có thể đặt một câu lệnh in trong một chức năng python không?
  • Làm cách nào để mở mã nguồn trong Python?

inspect.getsourcefile[print]
---------------------------------------------------------------------------
TypeError                                 Traceback [most recent call last]
 in []
----> 1 inspect.getsourcefile[print]

/usr/lib/python3.4/inspect.py in getsourcefile[object]
    569     Return None if no way can be identified to get the source.
    570     """
--> 571     filename = getfile[object]
    572     all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
    573     all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]

/usr/lib/python3.4/inspect.py in getfile[object]
    534         return object.co_filename
    535     raise TypeError['{!r} is not a module, class, method, '
--> 536                     'function, traceback, frame, or code object'.format[object]]
    537 
    538 ModuleInfo = namedtuple['ModuleInfo', 'name suffix mode module_type']

TypeError:  is not a module, class, method, function, traceback, frame, or code object

In [86]: inspect.getsourcefile[builtin.print]
---------------------------------------------------------------------------
NameError                                 Traceback [most recent call last]
 in []
----> 1 inspect.getsourcefile[builtin.print]

NameError: name 'builtin' is not defined

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

Defprint _ [*args, ** kwargs]:print_[*args, **kwargs]: print_[*args, **kwargs]:
"" "Chức năng in kiểu mới từ PY3K." ""
fp = kwargs.pop ["tệp", sys.stdout]=kwargs.pop["file", sys.stdout] = kwargs.pop["file", sys.stdout]
iffpisnone:fpisNone: fp is None:
return
defwrite [dữ liệu]:write[data]: write[data]:
ifnotisInstance [dữ liệu, basestring]:notisinstance[data, basestring]: not isinstance[data, basestring]:
data = str [dữ liệu]=str[data] = str[data]
fp.write [dữ liệu].write[data] .write[data]
want_unicode=False=False = False
sep = kwargs.pop ["sep", none]=kwargs.pop["sep", None] = kwargs.pop["sep", None]
ifsepisnotnone:sepisnotNone: sep is not None:
ifisInstance [sep, unicode]:isinstance[sep, unicode]: isinstance[sep, unicode]:
want_unicode=True=True = True
ElifnotisInstance [SEP, STR]:notisinstance[sep, str]: not isinstance[sep, str]:
RaisetypeError ["SEP phải là không có hoặc một chuỗi"]TypeError["sep must be None or a string"] TypeError["sep must be None or a string"]
end = kwargs.pop ["end", none]=kwargs.pop["end", None] = kwargs.pop["end", None]
ifendisnotnone:endisnotNone: end is not None:
ifisInstance [end, unicode]:isinstance[end, unicode]: isinstance[end, unicode]:
want_unicode=True=True = True
ElifnotisInstance [end, str]:notisinstance[end, str]: not isinstance[end, str]:
RaisetypeError ["Kết thúc phải là không có hoặc một chuỗi"]TypeError["end must be None or a string"] TypeError["end must be None or a string"]
ifkwargs:kwargs: kwargs:
raisetypeError ["đối số từ khóa không hợp lệ để in []"]TypeError["invalid keyword arguments to print[]"] TypeError["invalid keyword arguments to print[]"]
ifnotwant_unicode:notwant_unicode: not want_unicode:
forarginargs:arginargs: arg in args:
ifisInstance [arg, unicode]:isinstance[arg, unicode]: isinstance[arg, unicode]:
want_unicode=True=True = True
break
ifwant_unicode:want_unicode: want_unicode:
newline = u "\ n"=u"\n" = u"\n"
không gian = u ""=u" " = u" "
khác:: :
newline = "\ n"="\n" = "\n"
không gian = ""=" " = " "
ifsepisnone:sepisNone: sep is None:
sep=space=space = space
ifendisnone:endisNone: end is None:
end=newline=newline = newline
fori, arginenumerate [args]:i, arginenumerate[args]: i, arg in enumerate[args]:
nếu tôi:i: i:
Viết [SEP][sep] [sep]
viết [arg][arg] [arg]
Viết [kết thúc][end] [end]

Làm thế nào để bạn in mã nguồn của một hàm trong Python?

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. 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.use the getsource[] method of inspect module to get the source code of the function. Returns the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string.use the getsource[] method of inspect module to get the source code of the function. Returns the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string.

Làm cách nào để in mã lớp trong Python?

Trong Python, điều này có thể đạt được bằng cách sử dụng các phương thức __repr__ hoặc __str__.__repr__ được sử dụng nếu chúng ta cần một thông tin chi tiết để gỡ lỗi trong khi __str__ được sử dụng để in phiên bản chuỗi cho người dùng.Điểm quan trọng về in ấn: Python sử dụng phương thức __repr__ nếu không có phương thức __str__.using __repr__ or __str__ methods. __repr__ is used if we need a detailed information for debugging while __str__ is used to print a string version for the users. Important Points about Printing: Python uses __repr__ method if there is no __str__ method.using __repr__ or __str__ methods. __repr__ is used if we need a detailed information for debugging while __str__ is used to print a string version for the users. Important Points about Printing: Python uses __repr__ method if there is no __str__ method.

Bạn có thể đặt một câu lệnh in trong một chức năng python không?

Có, in [] sẽ in ra stdout.Có, nó vẫn sẽ xảy ra nếu bạn gọi chức năng trong một chức năng khác.. Yes, it will still happen if you call the function in another function.. Yes, it will still happen if you call the function in another function.

Làm cách nào để mở mã nguồn trong Python?

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

Bài Viết Liên Quan

Chủ Đề