我打算用Python中函数名称的字符串调用模块的函数,但每当我在程序中调用我的类时,它都会给我这个错误:TypeError:必须使用foo实例作为第一个参数调用unbound方法bar() (没有得到任何东西)
有人能帮我吗
我有一个字符串变量,它带有不同的函数名称,我有一个文件,其中包含一组与字符串内容匹配的常常不同的函数,如何在Python中调用该函数?
例:
在文件1中
def function1: ...
def function2: ...
def function3: ...
Run Code Online (Sandbox Code Playgroud)
在文件2中
functionname = "function2"
Run Code Online (Sandbox Code Playgroud)
我需要从这个文件中调用File1中的function2
对于实例我在python中有三个函数和一个for循环,我希望顺序执行所有这些函数,例如在第一次迭代时应该执行函数1和在第二次迭代函数2上执行等等
这三个功能是:
from scapy.all import *
from random import randint
import threading
import time
from datetime import datetime
import multiprocessing
from itertools import count
#pktList = []
#pktsInt = 0
#def Packets():
# Generate packet
#for run_no in range(0,1)
p = raw_input('Enter PACKETs to send: ')
pktsInt = int(p)
pkts = IP(src="10.0.0.1",dst="10.0.0.2")/TCP()/"GET /HTTP/1.0\r\n\r\n"/Raw(RandString(size=120))
#print pkts
pkts[TCP].flags = "UFP"
pktList = []
for pktNum in range(0,pktsInt):
pktList.extend(pkts)
pktList[pktNum][TCP].dport = 80
#randint(1,65535) # Pkt has Ran PortNo.
print pktList[pktNum].summary()
#print len(pktList[pktNum])
#wrpcap('tcp-packets.pcap',pktList[pktNum]) …Run Code Online (Sandbox Code Playgroud) 我希望能够在Python 2.7中执行以下操作:
for function in ['function1', 'function2', 'function3', 'function4']:
some_dictionary [function] = function (an_argument)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试这样做时,错误会增加.以下代码执行我希望上层代码执行的操作:
some_dictionary ['function1'] = function1 (an_argument)
some_dictionary ['function2'] = function2 (an_argument)
some_dictionary ['function3'] = function3 (an_argument)
some_dictionary ['function4'] = function4 (an_argument)
Run Code Online (Sandbox Code Playgroud)
是否有更紧凑的方式编写后一个代码,类似于前者?
提前致谢,
Logicum
我参考了以下主题,我不认为这篇文章与其中任何主题重复:
例如,假设我有一个字符串,data_type = "int"并且我想直接使用该字符串调用内置函数int。顺便说一句,我不能拥有data_type = int,因为data_type实际上是从 JSON 文件加载的,即data_type始终是字符串或None.
我最好的(最巧妙的)尝试是eval(data_type)("4"),但正如人们所建议的,这eval似乎不是一个好的选择,应该避免。
另一种方法是创建一个类似的字典data_type_dic = {"int": int, "float": float}并执行data_type_dic[data_type]("4")。然而,创建这本词典对我来说就像“重新发明轮子”。
由于int是内置函数,而不是模块中的方法,因此getattr似乎行不通。它也不是一个自定义函数,因此locals()[data_type]给出KeyError.
那么使用相应的字符串调用内置函数的最佳方法是什么?
我之前宣布了3个函数,这只是一个愚蠢的基于文本的cookie clicker-esque游戏.
dostuff={"" : turn() , "help" : helpf() , "invest" : invest() }
while done != True:<br>
do = input("What do you want to do? ")
do = do.lower()
if do == "" or do == "help" or do == "invest":
dostuff[do]
elif do == "quit":
done = True
Run Code Online (Sandbox Code Playgroud)
因此,当我使用dostuff["turn"]它什么都不做(该功能应该打印一些东西).我和其他选项有同样的问题.
我在Python 3.5中创建一个程序,当你输入不同的输入时它将运行不同的函数:
commandList = ["test1", "test2", "test3"]
def test1():
print("TEST 1 FUNCTION")
def test2():
print("TEST 2 FUNCTION")
def test3():
print("TEST 3 FUNCTION")
while True:
userRaw = input(">:")
user = userRaw.lower()
for x in range(len(commandList)):
if user == commandList[x]:
# Needs to run function which has the same name as the string 'user'
# E.g. if user = 'test1' then function test1() is run.
Run Code Online (Sandbox Code Playgroud)
在if声明之后我需要输入什么(评论在哪里)?
我尝试过做这样的事情,但是没有奏效:
commandList = ["test1", "test2", "test3"]
def function(test1):
print("TEST 1 FUNCTION")
def function(test2):
print("TEST …Run Code Online (Sandbox Code Playgroud)