我有几个函数可以对应用程序进行API调用。每个函数都被设置为返回json格式的信息。我声明了另一个函数来将json输出写入一个文件,以便在编码时保存。我在尝试传递函数以使API调用成为参数时遇到了问题。这有可能吗
class ApiCalls(object):
def __init__(self,
url='https://application.spring.com',
username='admin',
password='pickles',
path='/tmp/test/'):
self.url = url
self.username = username
self.password = password
self.path = path
def writetofile(self, filename, call):
if not os.path.exists(self.path):
os.makedirs(self.path)
os.chdir(self.path)
f = open(self.filename, 'w')
f.write(str(self.call))
f.close()
def activationkey(self):
credentials = "{0}:{1}".format(self.username, self.password)
url = self.url + '/katello/api/organizations/1/activation_keys'
cmd = ['curl', '-s', '-k',
'-u', credentials, url]
return subprocess.check_output(cmd)
x = ApiCalls()
x.writetofile('activationkey.json', activationkey())
是的,可以像其他对象一样传递函数
在您的特殊情况下,您混淆了函数的执行和函数本身
考虑下例中的
square
:您正试图将其作为
但你应该这么做
要回答上述非常具体的用例,您应该
相关问题 更多 >
编程相关推荐