我想在重新定义的函数中读入字典

时间:2013-02-04 16:04:24

标签: python dictionary

我想改变我的函数在代码中读取的方式,以便我可以导入一个包含更容易的托管变量的类。

我已经导入了所有要编辑的库。

class http_request(object):

  def __init__(self, website_address, valuedictionary):
   self.website_address = website_address
   self.valuedictionary = valuedictionary

  def get(self):
    return requests.get(website_address, params=valuedictionary)

  def post(self):
    return requests.post(website_address, data=valuedictionary)

def postContext(self):
    return requests.post(website_address, data=valuedictionary).context  
def getContext(self):
    return requests.get(website_address, params=valuedictionary).context

htay = http_request(web_add, payload)

print str(htay.postContext)

我的回答是: “绑定方法http_request.get of< main .http_request对象位于0x8735cec>>”

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我假设你想调用方法:

print str(htay.postContext())
#                         ^^ Need parenthesis to call a method

当然,这可能会给您一个关于global website_address not defined或类似内容的错误,因为在该方法中,您需要通过self获取实例属性:

def postContext(self):
    return requests.post(self.website_address, data=self.valuedictionary).context

您还需要对其他方法进行类似的更改。