-2
\$\begingroup\$

I have written below code to create/get a dict with n number of key, value pairs. Main idea is to use the same instantiated class object across my code and play around with k/v pairs.

Current implementation:

class DataForm:

    def __init__(self):
        self.data_dict = dict()

    def create_key_value_pair(self, key, value):
        self.data_dict[key] = value

    def get_dict(self):
        return self.data_dict

ob1 = DataForm()
ob1.create_key_value_pair("Jon", 28)
ob1.get_dict()

I was trying to understand and implement the above work(with some data validation) using getter/setter in Python.

class DataFormNew:

    def __init__(self):

        self._curr_dict = dict()

    @property
    def curr_dict(self):

        return self._curr_dict

    @curr_dict.setter
    def curr_dict(self, args):

        key, val = args

        if 0 < val < 100:
            self._curr_dict[key] = val

        else:
            raise ValueError("Value is not in range")

ob2 = DataFormNew()
ob2.curr_dict
ob2.curr_dict = ('Jack', 10)

Few points on which I would need clarification?


  1. Which approach is better?
  2. Am I trying to complicate a simple job by using python @property (getter/setter) ?
  3. In which scenario we should choose to implement our class with getter/setter?

PS: Actually it's not just about dict creation. I'm running my code on AWS EC2 server where for every task, there can be n number of files to read and write them in DB. For every file, there is going to be unique id which i'm storing in dict dynamically. Each of (filename, unique id) creates a key,value pair. Later, I have queries to update DB based on this unique id against every file. I'm using object oriented approach to simplify the task.

\$\endgroup\$
  • 1
    \$\begingroup\$ Why is obj = {} obj["Jon"] = 28 obj not ok? \$\endgroup\$ – Peilonrayz Apr 10 at 15:37
  • \$\begingroup\$ @Peilonrayz: Actually it's not just about dict creation. I'm running my code on AWS EC2 server where for every task, there can be n number of files to read and write them in DB. For every file, there is going to be unique id which i'm storing in dict dynamically. Each of (filename, unique id) creates a key,value pair. Later, I' have queries to update DB based on this unique id against every file. I'm not sure if i'm making much of a sense here! \$\endgroup\$ – Sumit Apr 10 at 15:47
2
\$\begingroup\$

Comparing the two, unless there is an outstanding reason to switch that I'm not seeing, I would keep using the first one. Not only is it more easily readable it falls under the principal of K.I.S.S. (keep it stupid simple).

A note on the first implimentation, did you mean for get_dict to return the entire dict? Or did you mean to do something like this, that returns the value for the specified key.

class DataForm:

    def __init__(self):
        self.data_dict = dict()

    def create_key_value_pair(self, key, value):
        self.data_dict[key] = value

    def get_dict(self, key):
        return self.data_dict.get(key)

ob1 = DataForm()
ob1.create_key_value_pair("Jon", 28)
ob1.create_key_value_pair("tim", 28)
print(ob1.get_dict('tim'))

`

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review. Whilst of the two solutions provided your answer is correct, I stand by my comment to the question that a plain old dictionary would be best. It would also better follow KISS. I suggest not answering questions with a negative score as you're likely to have a worse experience then if you were to answer a question with a positive score. Historically questions like this have lead to combative OPs. And some users, like myself, may downvote or not vote on an answer to an off-topic question. Thank you for the good answer, I hope to see you around. :) \$\endgroup\$ – Peilonrayz Apr 10 at 22:25
  • \$\begingroup\$ I didn't even think about it like that a plain dict would be the most simple, for some reason I had it in my head that it need to be a part of a class. As for answering negative questions I'm more or less just trying to answer what I can and try to help out. \$\endgroup\$ – Joe Smith Apr 10 at 22:39
  • \$\begingroup\$ It's all good Joe ^^ \$\endgroup\$ – Peilonrayz Apr 11 at 0:04
  • \$\begingroup\$ @JoeSmith: Thanks Joe for your valuable feedback! \$\endgroup\$ – Sumit Apr 12 at 6:55
  • \$\begingroup\$ @Peilonrayz: Thanks for the downvote. As I'm new to this platform, I was not aware of so called "Posting Questions guidelines" . Rest, be assured that it's not a hypothetical question which I have asked. Somehow, I do not have authority to post code's outside office space. Hence, I tried to ask in best possible way. Added to that a further PS had been added in question, to give an high level idea of task which includes similar piece of code. \$\endgroup\$ – Sumit Apr 12 at 6:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.