[Tutor] Defining functions
Kent Johnson
kent37 at tds.net
Sun Mar 27 14:07:48 CEST 2005
- print is just being changed to a function (instead of a statement), it's not going away entirely.
But for complete future compatibility I guess you would avoid it.
- I don't think you will ever get the prompt as part of the input unless the user actually types it
- You can strip the trailing newline easily
So here is my version:
import sys
def my_raw_input(prompt):
sys.stdout.write(prompt)
value = sys.stdin.readline()[:-1]
return value
BTW, this is an interesting exercise but I wouldn't actually code this way. Python 3.0 is a long
ways off and Python 2.x isn't going to go away when 3.0 arrives. (You can still download
six-year-old Python 1.5.2 from python.org!)
Here is a quote from a report of Guido van Rossum's keynote speech last week at PyCon:
"Guido started talking about Python 3000 in 2000. He said he always imagined it as a release that
was three years off, and it still feels that way."
http://pycon.blogspot.com/
I don't need raw_input() much in my code, but I use print without a second thought.
Kent
Jacob S. wrote:
> Try this.
>
> import sys
>
> def my_raw_input(prompt):
> sys.stdout.write(prompt) ## Since they're thinking of bonking
> off print as well.
> a = sys.stdin.readline()
> if a.startswith(prompt):
> return a[:len(prompt)]
> return a
>
> It leaves the '\n' on the end... so it sucks.
>
> I know there is a better way... Someone else-help?
>
> Jacob
>
>> So, as a newbie, I see this thread and I check out the PEP and I see
>> that for future compatibility we should use sys.stdin.readline(). So
>> I import sys to see how it works. Of course, sys.stdin.readline('type
>> anything: ') doesn't work in quite the same way as raw_input('type
>> anything: ') does. The closest I can get after a few newbie stabs is:
>>
>>>>> print 'type anything: ', sys.stdin.readline()
>>
>> type anything: hello
>> hello
>>
>>>>>
>>
>> What is the easiest way to get the exact functionality of raw_input()
>> (i.e. a prompt, no whitespace at the front, and no trailing \n) using
>> sys.stdin.readline()?
>>
>> gabe
>>
>>
>> On Fri, Mar 25, 2005 at 11:02:43AM -0500, Jacob S. wrote:
>>
>>> Yeah. And they're thinking of removing raw_input() too. I think it's
>>> good
>>> to have a __builtin__ user input function. Why should we have to import
>>> sys everytime we want user input? Almost every program that newbies
>>> write
>>> uses it, and advanced programmers also if they're using console
>>> programs.
>>> IMHO, I see no reason to remove it.
>>> ## end rant
>>>
>>> Jacob
>>>
>>>
>>> >Michael Dunn wrote:
>>> >>Something I've always wondered: if input() is so dangerous, why is it
>>> >>there? What valid uses does it have in the wild?
>>> >
>>> >It's a mistake planned to be removed in Python 3.0, the "hypothetical
>>> >future release of Python that can break backwards compatibility with
>>> the
>>> >existing body of Python code."
>>> >
>>> >Python tries very hard to maintain backward compatibility so things
>>> like
>>> >input() are not removed.
>>> >
>>> >http://www.python.org/peps/pep-3000.html#built-ins
>>> >
>>> >Kent
>>> >
>>> >_______________________________________________
>>> >Tutor maillist - Tutor at python.org
>>> >http://mail.python.org/mailman/listinfo/tutor
>>> >
>>> >
>>>
>>> _______________________________________________
>>> Tutor maillist - Tutor at python.org
>>> http://mail.python.org/mailman/listinfo/tutor
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list