0

I'm just getting back into Python after a long break. I'm running this code in 2.* Python but was using print() syntax. Don't think that should be an issue.

I don't understand while the final print statement in the function returns the correct answer but the "return" doesn't actually work and returns None/NoneType. Is this something I don't get about recursion in Python or recursion with staticmethods? Thanks. Code example below.

import json

class DictHelper:
    @staticmethod
    def get_info2(data, *args):
        k = args[0]
        print(data)
        if (len(args) > 1): 
            try:
                ndata = data[k]
                a = list(args)
                a.pop(0)
                DictHelper.get_info2(ndata, *tuple(a))
            except:
                print("Error with key = " + args[0])
        else:
            print("the value for data[" + k + "] is ")
            print(data[k]) # this works
            return data[k]

def demo():
    json_str = """{ "Payload": { "humidity": { "sensor-part-number": "X12345" }}}"""
    data = json.loads(json_str)
    v = DictHelper.get_info2(data, 'Payload', "humidity", "sensor-part-number")
    print(v) # this returns None

demo()
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

0 Answers0