J
jmfauth
What is input() supposed to return?
Python 3 made several backwards-incompatible changes over Python 2.
First of all, input() in Python 3 is equivalent to raw_input() in
Python 2. It always returns a string. If you want the equivalent of
Python 2's input(), eval the result. Second, Python 3 is now unicode
by default. The "str" class is a unicode string. There is a separate
bytes class, denoted by b"", for byte strings. The u prefix is only
there to make it easier to port a codebase from Python 2 to Python 3.
It doesn't actually do anything.
What is input() supposed to return?
True
:u'a'
(<class 'str'>, 4)
sys.argv?
It does. I shew it!
Incorrect. You are assuming that Python 3 input eval's the input like
Python 2 does. That is wrong. All you show is that the one-character
string "a" is not equal to the four-character string "u'a'", which is
hardly a surprise. You wouldn't expect the string "3" to equal the string
"int('3')" would you?
<SNIP>
A string is a string, a "piece of text", period. I do not see why a
unicode literal and an (well, I do not know how the call it) a "normal
class <str>" should behave differently in code source or as an answer
to an input().
Should a user write two derived functions? input_for_entering_text()
and input_if_you_are_entering_a_text_as_litteral() --- Side effect
from the unicode litteral reintroduction. I do not mind about this,
but I expect it does work logically and correctly. And it does not. PS
English is not my native language. I never know to reply to an
(interro)-negative sentence. jmf
A string is a string, a "piece of text", period.
I do not see why a unicode literal and an (well, I do not
know how the call it) a "normal class <str>" should behave
differently in code source or as an answer to an input().
Should a user write two derived functions?
input_for_entering_text()
and
input_if_you_are_entering_a_text_as_litteral()
Side effect from the unicode litteral reintroduction.
I do not mind about this, but I expect it does
work logically and correctly. And it does not.
I do not see why the semantic may vary differently
in code source or in an interactive interpreter,
esp. if Python allow it!
u'ajmfauth said:Thinks are very clear to me. I wrote enough interactive
interpreters with all available toolkits for Windows
You're using 2.x; this thread concerns 3.3, which, as has been repeatedu'a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SyntaxError: u'a
Er, no, not really
Thinks are very clear to me. I wrote enough interactive
interpreters with all available toolkits for Windows
since I know Python (v. 1.5.6).
I do not see why the semantic may vary differently
in code source or in an interactive interpreter,
esp. if Python allow it!
If you have to know by advance what an end user
is supposed to type and/or check it ('str' or unicode
literal) in order to know if the answer has to be
evaluated or not, then it is better to reintroduce
input() and raw_input().
You're using 2.x; this thread concerns 3.3, which, as has been repeated
several times, does not evaluate strings passed via input() like 2.x.
That code does not raise a SyntaxError in 3.x.
And you're missing the context. jmfauth thinks we should re-introduce
the input/raw-input distinction so he could parse literal strings. So
Jussi demonstrated that the 2.x input did NOT satisfy fmfauth's dreams.
Andrew said:You're using 2.x; this thread concerns 3.3, which, as has been
repeated several times, does not evaluate strings passed via input()
like 2.x. That code does not raise a SyntaxError in 3.x.
We are turning in circles.
You are somehow legitimating the reintroduction of unicode
literals
and I shew, not to say proofed, it may
be a source of problems.
Typical Python desease. Introduce a problem,
then discuss how to solve it, but surely and
definitivly do not remove that problem.
As far as I know, Python 3.2 is working very
well.
You are, not we. Please stop.
We are not 'reintroducing' unicode literals. In Python 3, string
literals *are* unicode literals.
Other developers reintroduced a now meaningless 'u' prefix for the
purpose of helping people write 2&3 code that runs on both Python 2 and
Python 3. Read about it herehttp://python.org/dev/peps/pep-0414/
In Python 3.3, 'u' should *only* be used for that purpose and should be
ignored by anyone not writing or editing 2&3 code. If you are not
writing such code, ignore it.
> and I shew, not to say proofed, it may
You are the one making it be a problem.
The simultaneous reintroduction of 'ur', but with a different meaning
than in 2.7, *was* a problem and it should be removed in the next release..
Except that many public libraries that we would like to see ported to
Python 3 have not been. The purpose of reintroducing 'u' is to encourage
more porting of Python 2 code. Period.
A string is a string, a "piece of text", period.
I do not see why a unicode literal and an (well, I do not know how the
call it) a "normal class <str>" should behave differently in code source
or as an answer to an input().
Should a user write two derived functions?
input_for_entering_text()
and
input_if_you_are_entering_a_text_as_litteral()
A string is a string, a "piece of text", period.I do not see why a unicode literal and an (well, I do not know how the
call it) a "normal class <str>" should behave differently in code source
or as an answer to an input().
They do not. As you showed earlier, in Python 3.3 the literal strings
u'a' and 'a' have the same meaning: both create a one-character string
containing the Unicode letter LOWERCASE-A.
Note carefully that the quotation marks are not part of the string. They
are delimiters. Python 3.3 allows you to create a string by using
delimiters:
' '
" "
u' '
u" "
plus triple-quoted versions of the same. The delimiter is not part of the
string. They are only there to mark the start and end of the string in
source code so that Python can tell the difference between the string "a"
and the variable named "a".
Note carefully that quotation marks can exist inside strings:
my_string = "This string has 'quotation marks'."
The " at the start and end of the string literal are delimiters, not part
of the string, but the internal ' characters *are* part of the string.
When you read data from a file, or from the keyboard using input(),
Python takes the data and returns a string. You don't need to enter
delimiters, because there is no confusion between a string (all data you
read) and other programming tokens.
For example:
py> s = input("Enter a string: ")
Enter a string: 42
py> print(s, type(s))
42 <class 'str'>
Because what I type is automatically a string, I don't need to enclose it
in quotation marks to distinguish it from the integer 42.
py> s = input("Enter a string: ")
Enter a string: This string has 'quotation marks'.
py> print(s, type(s))
This string has 'quotation marks'. <class 'str'>
What you type is exactly what you get, no more, no less.
If you type 42, you get the two character string "42" and not the int 42.
If you type [1, 2, 3], then you get the nine character string "[1, 2, 3]"
and not a list containing integers 1, 2 and 3.
If you type 3**0.5 then you get the six character string "3**0.5" and not
the float 1.7320508075688772.
If you type u'a' then you get the four character string "u'a'" and not
the single character 'a'.
There is nothing new going on here. The behaviour of input() in Python 3,
and raw_input() in Python 2, has not changed.
Should a user write two derived functions?input_for_entering_text()
and
input_if_you_are_entering_a_text_as_litteral()
If you, the programmer, want to force the user to write input in Python
syntax, then yes, you have to write a function to do so. input() is very
simple: it just reads strings exactly as typed. It is up to you to
process those strings however you wish.
len(input(':'))input(':')
:éléphant
'éléphant'input(':')
:u'éléphant'
'éléphant'input(':')
:u'\u00e9l\xe9phant'
'éléphant'input(':')
:u'\U000000e9léphant'
'éléphant'input(':')
:\U000000e9léphant
'éléphant' input(':')
:b'éléphant'
"b'éléphant'"
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.