0

I have a class with setter/getter methods, but the getter method doesn't return a value on my initial call:

import random

class Data:
    def __init__(self):
        self.data = {}

    def set_value(self, k):
        self.data[k] = random.random()

    def get_value(self, k):
        if self.data.get(k) is not None:
            return self.data.get(k)

        self.set_value(k)
        self.get_value(k)

Now if I initiate this class and call for a value like 4, on first call it assign a value for 4, but doesn't return it eventhou I'm calling it after the setter function:

d = Data()

d.get_value(4) # returns None

d.data # {4: 0.7578261616519488}

d.get_value(4) # returns 0.7578261616519488
1
  • maybe return get_value(self,k) in get_value method Commented Oct 22, 2020 at 16:56

1 Answer 1

1

You didn't return the value returned by the recursive call.

def get_value(self, k):
    if self.data.get(k) is not None:
        return self.data.get(k)

    self.set_value(k)
    return self.get_value(k)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.