变量未作为Arg[python]传递

变量未作为Arg[python]传递,python,Python,我试图根据接收到的输入自动执行函数,但当我尝试将输入作为和arg传递给函数时,出现了一个错误。这是我想做的一个例子 var ='hello world' def example(data): #function code example(var) 这是我所做的事情的一个基本用法,它返回一个错误,比如 var is not defined 这是我的实际代码 import AriaAudioConfig as Ariaconfig import AriaMathModule as A

我试图根据接收到的输入自动执行函数,但当我尝试将输入作为和arg传递给函数时,出现了一个错误。这是我想做的一个例子

var ='hello world'

def example(data):
    #function code

example(var)
这是我所做的事情的一个基本用法,它返回一个错误,比如

var is not defined
这是我的实际代码

import AriaAudioConfig as Ariaconfig
import AriaMathModule as AriaMath
import AriaLocationModule as AriaLocation
import AriaNLPModule as AriaNLP
from inspect import getmembers, isfunction
import re
import pandas as pd
import csv
from typing import Awaitable, Callable, TypeVar

location = ['geolocatecity','citydiff','locate', 'location', 'where is', 'far', 'distance']
math = ['calculate', 'add', 'subtract', 'multiply', 'divide', 'addition', 'subtraction', 'multiplication', 'division', 'square-root', 'power', 'squared', 'minus']
audio = ['volume','speak', 'sound']
nlp = ['translate', 'translation', 'language', 'english', 'spanish', 'french']
locdict = {'geolocatecity':'blabla','citydiff':'blabla'}

state = 0
city2 = 0
file = pd.read_csv('geolocations.csv')

def dataProcess(data):
    global state
    global city2
    datasearch = data.split()
    argsearch = datasearch
    datalength = len(datasearch)
    for i in range(datalength):
        if datasearch[i] in location:
            data = datasearch[i]
            datacom = typeremoval(functiongrep(AriaLocation))
            datacom = str(datacom).split()
            datalen = len(datacom)
            with open('geolocations.csv', 'rt') as f:
                reader = csv.reader(f, delimiter=',')
                for row in reader:
                    for field in row[0]:
                        for i in range(datalength):
                            if argsearch[i] == row[0]:
                                try:
                                    if city in locals():
                                        city2 = argsearch[i]
                                
                                except:
                                    city = argsearch[i]
                            
                            if argsearch[i] == row[1]:
                                    state = argsearch[i]
                                
                            if argsearch[i] == row[2]:
                                country = argsearch[i]
            
                f.close()
            
            for i in range(datalen):
                if str(data) in str(datacom[i]):
                    activefunction = datacom[i]
                    if state != 0:
                        eval('AriaLocation.' + activefunction +'(' + city + ',' + state + ',' + country + ')')
                    elif city2 != 0:
                        eval('AriaLocation.' + activefunction + '(' + city + ',' + city2 + ')')
                    else:
                        print('uh-oh something went wrong')
            
        elif datasearch[i] in math:
            data = datasearch[i]
            datacom = typeremoval(functiongrep(AriaMath))
            print(data)
            if data in datacom:
                print('found')
        elif datasearch[i] in audio:
            data = datasearch[i]
            datacom = typeremoval(functiongrep(Ariaconfig))
        elif datasearch[i] in nlp:
            data = datasearch[i]
            datacom = typeremoval(functiongrep(AriaNLP))

#dataProcess('Aria how far am I from Arizona')
def functiongrep(function):
    string = ''
    functions_list = [o for o in getmembers(function) if isfunction(o[1])]
    flen = len(functions_list)
    for i in range(flen):
        head, sep, tail = str(functions_list[i]).partition('<')
        string = string + head
    return string

def typeremoval(function):
    func = str(function)
    func = str(''.join(func))

    func = re.sub("[',()]", '', func)
    return func


dataProcess('locate Scottsdale Arizona USA')
我希望dataProcess根据作为输入的内容激活不同的命令

Exception has occurred: NameError
name 'Scottsdale' is not defined
File "/Users/timyc1/Desktop/DeadIdeas/smartroom/Seavernet/Aria/AriaProcessingModule.py", line 58, in dataProcess
eval('AriaLocation.' + activefunction +'(' + city + ',' + state + ',' + country + ')')
  File "/Users/timyc1/Desktop/DeadIdeas/smartroom/Seavernet/Aria/AriaProcessingModule.py", line 95, in <module>
dataProcess('locate Scottsdale Arizona USA')
不要为此使用eval。eval几乎从来都不是解决方案

if state != 0:
    getattr(AriaLocation, activefunction)(city, state, country)
elif city2 != 0:
    getattr(AriaLocation, activefunction)(city, cit2)
else:
    print('uh-oh something went wrong')

请共享错误回溯或发生错误的行,以及原始CALI编辑的带有错误的我的问题您在任意字符串上调用什么?您希望该语句做什么,特别是在您忽略返回值的情况下?@Brian我使用Eval从另一个模块调用函数。我按字符串调用该模块并不重要,因为该模块已定义。我强烈建议您阅读有关装饰器的内容,或者函数在Python中是如何成为一流公民的。functiongrep将生成一个长字符串,如“function1function2function3”,然后将其馈送到TypeRemoving,从中删除由于使用get_属性而不再存在的字符。。在这种情况下,了解装饰师可能会对您有所帮助!: