为什么调用此函数(python 用户输入的开关函数)?
Why is this function being called (python switch function for user input)?
我是 python 的新手,正在尝试设置一个命令行程序来根据用户输入运行一些不同的命令。用户输入将被发送到具有不同函数字典的函数。到目前为止,我的代码如下所示:`
不同命令选项的功能
def quit():
print("youre quiting")
ender = False
def ls():
print("You're listing files!")
命令函数的字典映射输入
def commands(argument):
switcher = {
"quit": quit(),
"ls": ls(),
3: "you typed 3",
}
return switcher.get(argument, "Invalid command")
主命令循环:获取用户输入,检查输入的参数数量并调用适当的命令函数
while ender == True:
userIn = input("okay go:")
userSplit = userIn.split()
numArgs = len(userSplit)
if numArgs == 0:
print("You must enter a command! Use the command 'help' for more information.")
elif numArgs >= 2:
print("You entered too many arguments! Use the command 'help for more information.")
else:
print(commands(userSplit[0])
无论我输入什么,它都会调用 ls 和 quit 函数并打印它们的语句。但是,如果我键入一个有效的无效命令并告诉我它无效(除了调用这两个函数)。
这是怎么回事?我怎样才能让它只调用我输入的函数?谢谢你的时间。
quit()
,例如。调用函数 then 并将 结果值 存储为字典值。只需存储函数 而无需 首先调用它:
switcher = {
"quit": quit,
"ls": ls,
"3": lambda: print("You typed 3!")
}
fn = switcher[cmd]
fn() # now call the selected function
将所有值设为 functions/lambdas 以实现调用的统一性,并对所有键使用 strings 以便找到它们。
构建字典时调用 ls()
而不是将键“ls”分配给 def ls()
的引用。
因此,每次调用 commands()
时都会构建字典,并且会调用 quit()
和 ls()
。你的字典在运行时变成
{
"ls": None,
"quit": None,
}
def test(argument):
def ls():
print("you're listing")
def lsCorrect():
print("you're listing from a function reference")
switcher = {
"ls": ls(),
"lsCorrect": lsCorrect,
}
return switcher.get(argument, "default")
print('first call')
# switcher[lsCorrect] => reference to lsCorrect, so we then invoke it with ()
test("lsCorrect")()
try:
print('second call')
# when constructing the dictionary ls() is invoked and that function returns None
# so we get a TypeError trying to call a function on None
test("ls")()
except TypeError:
print('tried to invoke something thats not a function')
我是 python 的新手,正在尝试设置一个命令行程序来根据用户输入运行一些不同的命令。用户输入将被发送到具有不同函数字典的函数。到目前为止,我的代码如下所示:`
不同命令选项的功能
def quit():
print("youre quiting")
ender = False
def ls():
print("You're listing files!")
命令函数的字典映射输入
def commands(argument):
switcher = {
"quit": quit(),
"ls": ls(),
3: "you typed 3",
}
return switcher.get(argument, "Invalid command")
主命令循环:获取用户输入,检查输入的参数数量并调用适当的命令函数
while ender == True:
userIn = input("okay go:")
userSplit = userIn.split()
numArgs = len(userSplit)
if numArgs == 0:
print("You must enter a command! Use the command 'help' for more information.")
elif numArgs >= 2:
print("You entered too many arguments! Use the command 'help for more information.")
else:
print(commands(userSplit[0])
无论我输入什么,它都会调用 ls 和 quit 函数并打印它们的语句。但是,如果我键入一个有效的无效命令并告诉我它无效(除了调用这两个函数)。
这是怎么回事?我怎样才能让它只调用我输入的函数?谢谢你的时间。
quit()
,例如。调用函数 then 并将 结果值 存储为字典值。只需存储函数 而无需 首先调用它:
switcher = {
"quit": quit,
"ls": ls,
"3": lambda: print("You typed 3!")
}
fn = switcher[cmd]
fn() # now call the selected function
将所有值设为 functions/lambdas 以实现调用的统一性,并对所有键使用 strings 以便找到它们。
构建字典时调用 ls()
而不是将键“ls”分配给 def ls()
的引用。
因此,每次调用 commands()
时都会构建字典,并且会调用 quit()
和 ls()
。你的字典在运行时变成
{
"ls": None,
"quit": None,
}
def test(argument):
def ls():
print("you're listing")
def lsCorrect():
print("you're listing from a function reference")
switcher = {
"ls": ls(),
"lsCorrect": lsCorrect,
}
return switcher.get(argument, "default")
print('first call')
# switcher[lsCorrect] => reference to lsCorrect, so we then invoke it with ()
test("lsCorrect")()
try:
print('second call')
# when constructing the dictionary ls() is invoked and that function returns None
# so we get a TypeError trying to call a function on None
test("ls")()
except TypeError:
print('tried to invoke something thats not a function')