Python - User input for name of a variable to pass as an argument to a function

痞子三分冷 提交于 2019-12-30 11:02:32

问题


I have a question here which is based around user input to scripts and passing this user-input to functions.

I have a script, within which I have defined a function. What I want the script to do is take user input to pass as arguments to the function. However, one of the things I want to pass to the function is the name of an argument rather than the argument itself. The user has the choice of using a variety of different lists to input to the function, and what I wanted was to get the user to input the name of the variable that they want to use.

Say I want to pass the argument Tablelist3 to the function. When I ask for the user input, and they input Tablelist3, what is being passed to the function is 'Tablelist3' as a string, rather than the variable itself.

How do I get it so that whichever variable the user names is the variable which gets passed to the function?

Hope my question makes sense and isn't too simple. I'm relatively inexperienced with Python.


回答1:


Use a dictionary, mapping strings to objects:

tbl1, tbl2 = [1 ,2 ,3], [4 ,5 ,6]
args = {'tbl1': tbl1 ,"tbl2" :tbl2}
# show tables ......

inp = input("Choose table")


def foo(var):
    print(var)


foo(args[inp])

You will want to do error checking to make sure the user actually enters something valid.




回答2:


I think you can take advantage of python's global() or locals() feature.

here's an example:

from random import choice as rc



def your_function(variable):
    if variable in local_keys:
        print(variable, " = ", local_variables[variable])
    else:
        print("Your input is not a defined variable")
    return

#randomly populate the variables
item1 = rc(range(20))
item2 = rc(range(20))
item3 = rc(range(20))
item4 = rc(range(20)) 
item5 = rc(range(20))
item5 = rc(range(20))

user_variable = input("Input the variable name: ")

local_variables = locals() # you may have to use globals instead
local_keys = list(local_variables.keys())

your_function(user_variable)



回答3:


with raw_input the input is just a string

table2 = range(3)
variable_name = raw_input('Enter Variable Name: ')
def function(list_object):
    print list_object

function(globals()[variable_name])

with input() the input data given by the user is evaluated, be sure that the all the possible input lists are declared before asking for user input, other wise you end up getting an NameError

table2 = range(3)
variable_name = input('Enter Variable Name: ')
def function(list_object):
    print list_object

function(variable_name)

globals() function returns a dictionary of all the objects defined in the module. you can get the respective object by passing the variable name as a key.



来源:https://stackoverflow.com/questions/34177769/python-user-input-for-name-of-a-variable-to-pass-as-an-argument-to-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!