Hướng dẫn print function python source code - chức năng in mã nguồn 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.

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):
"" "Chức năng in kiểu mới từ PY3K." ""
fp = kwargs.pop ("tệp", sys.stdout) = kwargs.pop("file", sys.stdout)
iffpisnone: fp is None:
return
defwrite (dữ liệu): write(data):
ifnotisInstance (dữ liệu, basestring): not isinstance(data, basestring):
data = str (dữ liệu) = str(data)
fp.write (dữ liệu).write(data)
want_unicode=False = False
sep = kwargs.pop ("sep", none) = kwargs.pop("sep", None)
ifsepisnotnone: sep is not None:
ifisInstance (sep, unicode): isinstance(sep, unicode):
want_unicode=True = True
ElifnotisInstance (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")
end = kwargs.pop ("end", none) = kwargs.pop("end", None)
ifendisnotnone: end is not None:
ifisInstance (end, unicode): isinstance(end, unicode):
want_unicode=True = True
ElifnotisInstance (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")
ifkwargs: kwargs:
raisetypeError ("đối số từ khóa không hợp lệ để in ()") TypeError("invalid keyword arguments to print()")
ifnotwant_unicode: not want_unicode:
forarginargs: arg in args:
ifisInstance (arg, unicode): isinstance(arg, unicode):
want_unicode=True = True
break
ifwant_unicode: want_unicode:
newline = u "\ n" = u"\n"
không gian = u "" = u" "
khác::
newline = "\ n" = "\n"
không gian = "" = " "
ifsepisnone: sep is None:
sep=space = space
ifendisnone: end is None:
end=newline = newline
fori, arginenumerate (args): i, arg in enumerate(args):
nếu tôi: i:
Viết (SEP)(sep)
viết (arg)(arg)
Viết (kết thúc)(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.

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.

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.

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

Nhấp đúp vào a.Tập lệnh PY sẽ thực thi tập lệnh (và không thể gửi bất kỳ kết quả hữu ích nào đến màn hình).Để xem nguồn, bạn có thể mở a.Tệp PY với IDLE (đi kèm với Python) hoặc thậm chí Notepad mặc dù nên sử dụng trình soạn thảo văn bản hoặc IDE nâng cao hơn.