【发布时间】:2015-06-14 02:01:37
【问题描述】:
我有一个班级的以下代码:
class _Getch:
def __init__(self):
self.impl = _GetchWindows()
def read_key(self):
return self.impl()
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
然后我有另一个导入 _Getch 的类。在这个其他类中,我尝试使用 _Getch 提供的 read_key 来做有条件的事情:
r = _Getch()
key = r.read_key()
print(key)
if key = 'a':
#do things
elif key = 's':
# do other things
else:
continue
当我尝试输入 'a' 时,我希望 key 是 'a',但它却返回了 b'a'。因此,key 不会满足任何条件,并且总是会继续。为什么它返回 b'a'?我该怎么做才能让它返回“a”?
【问题讨论】:
标签: python-3.x msvcrt getch