简体   繁体   English

python 字典与 function 和 arguments

[英]python dictionary with function and arguments

I want to use a dictionary to call functions with arguments, ie this simple program.我想用字典来调用arguments的函数,即这个简单的程序。 I'm a very novice coder in python.我是 python 中的一个非常新手的编码器。 what am I doing wrong?我究竟做错了什么? output: output:

what name? : s
what name? : f


f



s

want to write or read? w/r: 
w
want singers or band members? s/b: 
s
1
 

code:代码:

def addToFile(filename, lineString):
  file = open(filename,"a")
  file.write(lineString + "\n")
  file.close()
  return 1
    
def readFile(filename):
  file = open(filename)
  for line in file:
    print(line)
  file.close()
  return 2

whatToDo = {
  "ws": addToFile("band.txt",input("what name? : ")),
  "wb": addToFile("singers.txt",input("what name? : ")),
  "rs": readFile("singers.txt"),
  "rb": readFile("band.txt")
}
def promptWrite():
  print("want to write or read? w/r: ")
  choice = str(input())
  print("want singers or band members? s/b: ")
  choice += str(input())
  val = whatToDo[choice]
  print(val)

promptWrite()  

I didn't know If I needed to have a value or something, so I put the returns in the functions and had val.我不知道我是否需要一个值或什么东西,所以我把返回值放在函数中并有 val。 That isn't nessisary, I just drafted this up as an example program.这不是必需的,我只是将其起草为示例程序。 I know that you can have a dictionary with the names of the functions and call dictionaryName[whateverthing]() to run the function, but I don't know how to have the arguments vary in that我知道您可以拥有一个包含函数名称的字典并调用dictionaryName[whateverthing]()来运行 function,但我不知道如何让 arguments 在这方面有所不同

You're calling the functions when you create the dictionary, not when you access the dictionary.您在创建字典时调用函数,而不是在访问字典时调用。 Use lambda to create anonymous functions, then add () when you fetch from the dictionary to call it.使用lambda创建匿名函数,然后在从字典中获取时添加()调用它。

def addToFile(filename, lineString):
  file = open(filename,"a")
  file.write(lineString + "\n")
  file.close()
  return 1
    
def readFile(filename):
  file = open(filename)
  for line in file:
    print(line)
  file.close()
  return 2

whatToDo = {
  "ws": lambda: addToFile("band.txt",input("what name? : ")),
  "wb": lambda: addToFile("singers.txt",input("what name? : ")),
  "rs": lambda: readFile("singers.txt"),
  "rb": lambda: readFile("band.txt")
}
def promptWrite():
  print("want to write or read? w/r: ")
  choice = input()
  print("want singers or band members? s/b: ")
  choice += input()
  val = whatToDo[choice]()
  print(val)

promptWrite()  

You can't set functions with parameters to be executed in a dictionary, but you can store functions to call them after, with the corresponding dict key and parenthesis.您不能将带有参数的函数设置为在字典中执行,但您可以存储函数以在之后调用它们,并使用相应的 dict 键和括号。 Example:例子:

my_dict['write_output'] = print
my_dict['write_output']('hello from my key')

IMHO, storing your functions in dict keys is a design incosistency.恕我直言,将您的功能存储在 dict 键中是一种设计不一致。 Dicts are mutable objects, then someone could overwrite your keys without any restriction and mess up your code everywhere.字典是可变对象,然后有人可以不受任何限制地覆盖您的密钥,并在任何地方弄乱您的代码。 Not a good idea at all.根本不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM