user4130410
user4130410

Reputation:

call a method as argument in other method - python

I am new to python PL. i have a doubt like this:

my method:

def preprocess(json_data):
    for rates in json_data['query']['results']['rate']:
        map_dict
        map_dict['rate'] = rates['Rate']

and i wanted to pass this method as argument in other method who has json_data variable.

def read_currency_data(self, request_url, response_type, preprocess=None):
    parse_url = urllib.urlopen(request_url).read()
        json_data = json.loads(parse_url)
        json_data.preprocess(json_data)

So, as i call preprocess(json_data) it should evaluate my json_data variable. my existing code gives traceback:

ValueError: "'dict' object has no attribute 'preprocess'"

Please guide me to achieve my goal. thanks in advance

Upvotes: 0

Views: 45

Answers (1)

Amadan
Amadan

Reputation: 198324

Change

    json_data.preprocess(json_data)

into just

    preprocess(json_data)

because, as the error message says, json_data is a dict object, and when you do json_data.preprocess you're trying to find a method called preprocess on the dict class, which is not defined. Instead, you need a function (not a method).

Upvotes: 2

Related Questions