Can you create a new variable in a class by using an input from user?


#1

Hi everyone. I'm just wondering if we can create a user input syntax and anything that the user types in there will be added to a class, creating a new variable. The reason why I asked is because I encountered an online exercise wherein the user is tasked to create a certain code for a certain scenario. So the scenario goes like I'm a seller and I'm adding products on my menu. It will ask me for the brand, type, and price of my product until I typed in some sort of keyword like "stop" to stop the prompt. Then there's another option, this time for the buyer, to search through the items i placed on my catalog. I'm just wondering, how should i do it?? My initial idea is to create a class for a buyer and seller and put the add_product method under the seller and search_product method under buyer. But the thing is, not until I type in "Stop", the interface will keep asking me details of the product. Now my question is, where do I store those input answers from me in a way that it can be used by the buyer class for searching? Did I confuse you? Sorry, that's the best that I could explain it.. I really hope someone here could help me out.. Thanks in advance..


#2

Those attributes are stored in a dictionary, but if you only need the object for its dictionary, then use a dictionary instead, or perhaps inherit dictionary if you wish to add additional functionality. (As a bonus you'll avoid unexpected behaviour when names collide with already existing attributes)


#3

@migs08 @ionatan

Yup, a dict would be great for something like that.

def get_input():
    return input("Enter Stuff)

class HoldInput(object):
    def __init__(self):
        self.input_holder = dict()

    def store_input(self):
        self.input_holder[get_input()] = get_input()

Something like that, while you will need a few checks to ensure continuity and the such. Best of luck!


#4

Hi! Thanks so much for your quick reply! I'll do some experimenting first with that idea and I'll just ask questions along the way. Thank again! Really appreciate it!


#5

Hi! That's a great code example that you gave me! Thanks a lot!! Really appreciate it! :smiley:


#6

I should also mention depending on what you intend to do with your info, investing a little time into learning how to use XML or a database module to store your data in a persistent manner would be better than this though all you have to do to save your information and load from it is as follows.

Example of saving class data.

from os.path import join as join_
from os import getcwd
from os import makedirs

_path = getcwd()
_path_save = join_(_path, "SaveData")

def _create_dirs():
    if not path.exists(_path_save):
        makedirs(_path_save)
   
def get_input():
    return input("Enter Stuff")

class HoldInput(object):
    def __init__(self):
        self.input_holder = dict()

    def store_input(self):
        self.input_holder[get_input()] = get_input()

    def save_data(self):
        with open(join_(_save_data_, "hold_save_data.txt", "a+")) as file:
            file.write(str(self.__dict__))

    def load_data(self):
        with open(join_(_save_data_, "hold_save_data.txt", "a+")) as file:
            file.seek(0)
            self.input_holder = eval(file.read())

While I would not really recommend using eval because it is unsafe, if you use 3.* you can use literal_eval() which is safe. Now this function as it is written will only work for this function, if you have more data stored than this or in different types() you will have to adjust accordingly.

The easiest way to see how you need to do so is to just save your classes __dict__ that has all the variable information from your class. Non of your methods are saved just the variables.