Search code examples
pythonclassoopinitializationinstantiation

The most Pythonic way to create an object with user inputs


I was wondering what is the most Pythonic way to create an object whose parameters come from user inputs. Consider the example:

class SomeClass:
    def __init__(self, a, b, c):
        self.x= a
        self.y= b
        self.z= c

Should I have functions elsewhere (in another module?) that asks the user to input values for a, b, c? Alternatively, should I set x, y, z to None and then have helper functions called from the init function that prompt the user to set the values for the parameters? What is the most Pythonic way to accomplish this? I've been working with Python for a few years but am (finally) starting prioritize style and code aesthetics.

Thank you for your assistance.


Solution

  • You could wrap it up with this statement

    class SomeClass:
        def __init__(self, a, b, c):
            self.x= a
            self.y= b
            self.z= c
           
           
    if __name__ == "__main__":
        a = input("Enter a: ")
        b = input("Enter b: ")
        c = input("Enter c: ")
        some_class = SomeClass( a , b, c )