python class arguments with user input

Question:

I’m new to OOP in python and have learnt how to make a simple class and how to pass arguments to it.
My question is using arguments when a class accepts user inputs.

by default, my code is like this

class Simple:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f"{self.name},{self.age}"

x = Simple("Jake",12)
print(x)

I want to modify this code so that a user can input their name and age instead of giving it a predefined value.

I just assign self.name and self.age to a user input

class Simple:
    def __init__(self, name, age):
        self.name = input("Please enter name: ")
        self.age = input("Please enter age: ")

and when using Simple, I just need to call it without giving it any arguments.

x = Simple()
print(x)

But using the same method, I can just get rid of name and age arguments and rewrite the __init__ function as

def __init__(self):
    self.name = input("Please enter name: ")
    self.age = input("Please enter age: ")

If this works, then what’s the point of declaring arguments other than self?

Asked By: Aryan

||

Answers:

Declaring arguments other than self in the __init__ function is important because it allows you to pass arguments to the class constructor when you create an instance of the class.

In your first example, when you create an instance of the Simple class, you pass two arguments: name and age, like this:

x = Simple("Jake", 12)

These arguments are used to set the name and age attributes of the instance.

If you remove the arguments from the __init__ function and just use input to get the values, you won’t be able to pass them as arguments when you create an instance of the class. Instead, you will always have to prompt the user for the values, which may not always be desirable.

So, it’s best to keep the arguments in the __init__ function and use them to set the attributes of the instance. This way, you can create instances with different values for name and age by passing different arguments when you create them.

Answered By: Heklon

I would use a class to represent a type of data and not where the data is coming from. By declaring other arguments, I can choose whether these arguments are going to be feed by the user or from any other source.

Nothing stops you from doing

class Simple:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f"{self.name},{self.age}"

name = input("foo")
age = int(input("bar"))

x = Simple(name, age)

And now your Simple class can be also used with data coming from a text file, an excel sheet, previous calculations from your program, an HTML form or any other fancier user interface than the one provided by input.

Answered By: Ignatius Reilly

When you create an instance of the Simple class, you no longer pass any arguments to the init method. Instead, the init method prompts the user for input to initialize the object’s attributes. While this modification works, it means that you can no longer initialize the object’s attributes with specific values when you create an instance of the Simple class. You always have to rely on user input to initialize the object’s attributes.

To allow for both specific values and user input to initialize the object’s attributes, you can modify the init method to accept arguments with default values. E.g.:

class Simple:
    def __init__(self, name=None, age=None):
        if name is None:
            self.name = input("Please enter name: ")
        else:
            self.name = name
        
        if age is None:
            self.age = input("Please enter age: ")
        else:
            self.age = age

Now if you create an instance of the Simple class without passing any arguments, the init method prompts the user for input to initialize the object’s attributes:

x = Simple()

If you create an instance of the Simple class with arguments, the init method initializes the object’s attributes with those arguments:

x = Simple("Jake", 12)

By accepting arguments with default values, you allow for more flexibility in how the object is initialized. You can choose to initialize the object’s attributes with specific values or rely on user input to initialize them.

Answered By: Saman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.