0

I've built a class to ask a user a question, based on a type.

class Question:

    def __init__(self, subject):
        self.subject = subject
        self.question = f"Enter the {subject} to be created. You may end this by typing 'DONE':\n"
        self.still_needed = True

    def ask_question(self):
        ans_list = []
        running = True
        while running:
            var = input(f"Enter {self.subject}?\n")
            if var.lower() == 'done':
                running = False
            else:
                ans_list.append(var)
        return ans_list

The idea is to have a question model, to create lists of items.

This seems to work well with the following code in main.

roles = Question(subject="role").ask_question()

This creates a list from the Queue Class and uses it's method ask question to generate the list. As far as I can tell the object is then destroyed, as it's not saved to a variable.

My question, being new to Python and OOP is, does this seem like a solid and non-confusing way, or should I refractor? If so, what does the community suggest?

  • 1
    What do you need a class for if you don't keep the object? All you use it for is to call a method of this object as if it were a function. So, just define it as a function. A class would make sense if you kept one or several objects for future reference. – Martin Wettstein May 08 '21 at 13:23

1 Answers1

0

MY OPINION

I guess it depends on you. For one, one of the main purposes of using a class is to create an instance with it later on. Classes are objects ,or "categories" as I like to call them, that you use when there are distinctive types of instances in your project.

Given your code snippet, I can't really suggest anything, I don't know the usage of self.question and self.still_needed. However, if I were to base my opinion on just this part: roles = Question(subject="role").ask_question(), then I'd definitely go with using a function instead. As you've said,

As far as I can tell the object is then destroyed, as it's not saved to a variable.

ALTERNATIVE SOLUTION

Use decorators → the one with @ symbol

In this case, @staticmethod is the way to go!

What are staticmethods? The staticmethod decorator is a way to create a function in a class. So instead of it becoming a method, it can be treated as a function (without self parameter). This also means that a static method bounds to the class rather than its object. Consequently, static methods do not depend on objects (hence, you don't need to create an object for you to use it). Example:

class SomeMathStuff():
    @staticmethod
    def AddSomeNumbers(iterable):
        return sum(iterable)
 
result = SomeMathStuff.AddSomeNumbers([1, 2, 3])
# result = 6

As you can see, I did not need to create an object, instead I just needed to call its class to use it. Word of warning, most Python programmers argue that this is the un-Pythonic way, but I wouldn't worry too much about it. Hell, even I use these methods sometimes. In my defense, this is a good and efficient way to organize your project. With this, you can apply class methods globally and you can "categorize" them in certain classes you find suitable.

Anyway, this is all I have! I apologize if I misinformed you in any way.

ADDITIONAL INFROMATION ... in case I wasn't the best teacher