파이썬에서 '인쇄'란 무엇입니까?
나는 무엇을하는지 이해 print
하지만 그 언어 요소는 어떤 "유형"인가? 함수라고 생각하는데 왜 실패할까요?
>>> print print
SyntaxError: invalid syntax
되지는 print
함수? 이렇게 인쇄해야하지 않습니까?
>>> print print
<function print at ...>
2.7 이하에서는 print
성명서입니다. 파이썬 3에서는 print
함수입니다. Python 2.6 또는 2.7에서 인쇄 기능을 사용하려면 다음을 수행하십시오.
>>> from __future__ import print_function
>>> print(print)
<built-in function print>
참조 이 섹션 파이썬 언어 참조 설명서에서뿐만 아니라 PEP (3105) 가 변경 이유를합니다.
Python 3에서는 print()
내장 함수 (객체)입니다.
이 전에 print
이었다 문 . 데모...
파이썬 2. x :
% pydoc2.6 print
The ``print`` statement
***********************
print_stmt ::= "print" ([expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]])
``print`` evaluates each expression in turn and writes the resulting
object to standard output (see below). If an object is not a string,
it is first converted to a string using the rules for string
conversions. The (resulting or original) string is then written. A
space is written before each object is (converted and) written, unless
the output system believes it is positioned at the beginning of a
line. This is the case (1) when no characters have yet been written
to standard output, (2) when the last character written to standard
output is a whitespace character except ``' '``, or (3) when the last
write operation on standard output was not a ``print`` statement. (In
some cases it may be functional to write an empty string to standard
output for this reason.)
-----8<-----
Python 3. x :
% pydoc3.1 print
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
print
is a mistake that has been rectified in Python 3. In Python 3 it is a function. In Python 1.x and 2.x it is not a function, it is a special form like if
or while
, but unlike those two it is not a control structure.
So, I guess the most accurate thing to call it is a statement.
In Python all statements (except assignment) are expressed with reserved words, not addressible objects. That is why you cannot simply print print
and you get a SyntaxError
for trying. It's a reserved word, not an object.
Confusingly, you can have a variable named print
. You can't address it in the normal way, but you can setattr(locals(), 'print', somevalue)
and then print locals()['print']
.
Other reserved words that might be desirable as variable names but are nonetheless verboten:
class
import
return
raise
except
try
pass
lambda
In Python 2, print
is a statement, which is a whole different kind of thing from a variable or function. Statements are not Python objects that can be passed to type()
; they're just part of the language itself, even more so than built-in functions. For example, you could do sum = 5
(even though you shouldn't), but you can't do print = 5
or if = 7
because print
and if
are statements.
In Python 3, the print
statement was replaced with the print()
function. So if you do type(print)
, it'll return <class 'builtin_function_or_method'>
.
BONUS:
In Python 2.6+, you can put from __future__ import print_function
at the top of your script (as the first line of code), and the print
statement will be replaced with the print()
function.
>>> # Python 2
>>> from __future__ import print_function
>>> type(print)
<type 'builtin_function_or_method'>
참고URL : https://stackoverflow.com/questions/7020417/what-is-print-in-python
'Nice programing' 카테고리의 다른 글
옵션 유형 목록을 하나도없는 요소로 압축하는 가장 좋은 방법은 무엇입니까? (0) | 2020.11.30 |
---|---|
쉘 스크립트에서 원격 파일 크기를 얻는 방법은 무엇입니까? (0) | 2020.11.30 |
MicrosoftAjax.js, MicrosoftMvcAjax.js 및 MicrosoftMvcValidation.js는 ASP.NET MVC 3에서 더 이상 사용되지 않습니까? (0) | 2020.11.30 |
TextBox를 편집 불가능하게 만들기 (0) | 2020.11.30 |
C ++ 코드에서`???-`의 의미 (0) | 2020.11.30 |