python without OO
claudio.grondi at freenet.de
Wed Jan 26 05:26:08 EST 2005
I can't resist to point here to the
Re: How to input one char at a time from stdin?
posting in this newsgroup to demonstrate, what
this thread is about.
> >On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
> ><brent.hughes at comcast.net> wrote:
> >> I'd like to get a character from stdin, perform some action, get
> >> character, etc. If I just use stdin.read(1), it waits until I finish
> >> a whole line before I can get the first character. How do I deal with
> >This is exactly what you need:
> >Title: "getch()-like unbuffered character reading from stdin on both
> >Windows and Unix"
> Nice to know how, but all those double underscores made my eyes bleed.
> Three classes? What's wrong with something simple like the following
> (not tested on Unix)?
> import sys
> bims = sys.builtin_module_names
> if 'msvcrt' in bims:
> # Windows
> from msvcrt import getch
> elif 'termios' in bims:
> # Unix
> import tty, termios
> def getch():
> fd = sys.stdin.fileno()
> old_settings = termios.tcgetattr(fd)
> ch = sys.stdin.read(1)
> termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
> return ch
> raise NotImplementedError, '... fill in Mac Carbon code here'
"Davor" <davorss at gmail.com> schrieb im Newsbeitrag
news:1106689788.676121.48490 at c13g2000cwb.googlegroups.com...
> Is it possible to write purely procedural code in Python, or the OO
> constructs in both language and supporting libraries have got so
> embedded that it's impossible to avoid them? Also, is anyone aware of
> any scripting language that could be considered as "Python minus OO
> stuff"? (As you can see I'm completely new to Python and initially
> believed it's a nice&simple scripting language before seeing all this
> OO stuff that was added in over time)
Here the OO "solution" (activestate recipe 134892):
"""Gets a single character from standard input. Does not echo to the
self.impl = _GetchWindows()
self.impl = _GetchUnix()
def __call__(self): return self.impl()
import tty, sys
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
ch = sys.stdin.read(1)
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
getch = _Getch()
More information about the Python-list