LoginSignup
0
0

More than 5 years have passed since last update.

python3x: print vs evaluate

Posted at

piazzaでこんな質問を見つけた。

I don't understand why print removes the quotations marks from its operand(s), whereas simple evaluation does not.

For instance,

>> print('xyz')
xyz

but

>> 'xyz'
'xyz'

I feel like this has something to do with the fact that what print displays is a side effect, not its output. Also, I imagine this has to do with the subtle distinction between expression and value for objects of . But for the life of me I don't see how it all ties together.

たしかに考えてみたらpython interpreter上だとprintを使った時と単にevaluation(print抜きで中身だけを入れた場合)とでは中身は同じだがprintの時だけクォーテーションマークがなくなって表示される。

That's exactly it! print means "take this string and paste it into the terminal". On the other hand, when you type an expression that evaluates to a string, the Python interpreter displays a representation of that value. Since strings are surrounded with quotes in their Python representation, it shows the quotes.

The print function returns the special value None, which is not displayed by the interpreter:

>> None
>> # no output, asking for more input

so when you print a string, it has the side effect of outputting to the terminal, and the final value is None, which is ignored by the interpreter.

We'll learn a little bit later in the course that what the interpreter does is print(repr(value)), where repr is a function that takes a value and returns a string, the Python representation of that value:

>> 5
5
>> repr(5)
'5'
>> print(repr(5))
5

ということはprint("xyz")とinterpreter上に"xyz"とうって返ってきた値はそれぞれ違うものだと解釈することができる。クォーテーションマークもvalueの一部でありpython interpreterにおいてはstringはstandard way of representing valueであると。

I feel like there's still a missing piece for me here, regarding expressions and values. There seems to be something Platonic going on. That is, the Python interpreter can't actually display a value, right? The sense I have is that a value is something floating out in the "Platonosphere" (or more likely, a collection of bits). I imagine that what the interpreter displays is akin to a canonical expression for a particular value. Is this at all the case?

So when I input

>> 5 + 2

I get

>> 5 + 2
7

It's not that 7 "is" the value of this expression; rather, it's that 7 is the standard way of expressing the value that 5 + 2 represents. At least, this is how I've been thinking of it.

この質問への回答で割りとクリアになってくる:

That's a good way of thinking about it. The interpreter can only print strings. It's the job of the repr function to turn that abstract Python value into a string which can be outputted to the terminal. Here's some examples:

>> repr(5)  # returns a string with one character, the digit 5
'5'
>> repr('hello')  # puts quotes around hello; in the output, the single quotes are part of the string
"'hello'"

It's correct to say something like "7 is the value of the expression 2 + 5", because we mean the integer 7 (an abstract Python value). The string '7' is the standard representation of the integer 7, so the string '7' gets printed out.

つまりpython interpreter上ではストリング以外(全てかどうかはわからないが)はabstract valueとして解釈されている。それをthe standard representation of the valueにするにはストリングにする必要がある。そのためrepr(as in reprsentation of that value)ということなんですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0