Python does not have a keypress capture so you have to use one provided by the system like termios.
import termios, sys, os
TERMIOS = termios
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new = new & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[TERMIOS.VMIN] = 1
new[TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = None
c = os.read(fd, 1)
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
if __name__ == '__main__':
print "type something...'q' to quit"
s = ''
c = getkey()
if c == 'q':
print "captured key", c, ord(c)
s = s + c
C programmers don't like using "new" as there is a new keyword and an operator new in C++. They also insist on the redundant use of main(). Perhaps someone will write a "Python is not C" article. Until then, I will try to restrain from using variable names that make others uncomfortable. Hopefully, they will restrain from interrupting the program flow by making the reader jump back up into the program to find main() in order to start at the beginning, which is in the middle.
Do you recommend new to be used as a basic variable or reserved?
From python point of view.?
I strongly dont recommend that expecially as python seems still evolving. Changes here and there. Who knows.?
But i need your view Grib.!
I would not recommend new as a variable name because when you think of using new as a variable, there are usually better and more explicit alternatives. The second argument is the same as yours: python changes and new is a potential future keyword (I don't think the risk is very high because if new is not a keyword in python, it's probably a deliberate choice of the python developpers).
The use of new in woooee's snippet above is reasonable: in a local scope when there are an 'old' value and a 'new' value. Why not ? 'old_state' and 'state' were other candidates.
In the end. I think its more prudent to reserve new as a keyword and using new must be avoided due to various reasons. I name a few.
1.Print has been a function in numerous prog. languages(c,c++,php,perl) but python introduced this idea in py3.
2. Python is all object variable types(python's way). Thus py variables are more than C,C++, variables. They are objects. Objects are initialized with new keyword. PYc. Foundermental python written language.
OK. This is my concern.
New stuffs that python started including in python 3 are already up and actived stuff in other languages.. or the above listed, haskell and partly lua.
Print function is something they could have included from the start but they did not see why.
New is on the way... Trust me. I have been carefuly analyzing python and its future and i think. This change is bound to happend.
Besides python is gradually moving to te top. Python could have been in place of java if all these litle flavours of python came under python with single install. eg(ironpy,numpy,pyc,matpy,pyaudio,pymol) you name it.
Java is about 140mb on my system. Due to that, java runs everywhere. what is the point of 20mb and strugle all over.?
Python is not only a prog. language . IT IS THE FUTURE !!!