It is not so much using a string as an argument. When you are setting attributes of a class you need to reference them in your code. Take this example:
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
employee_1 = Employee
add_name = input('Enter name:> ')
add_age = int(input('Enter age:> '))
employee_1.name = add_name
employee_1.age = add_age
print('Employee name: ', employee_1.name, '\nEmployee age: ', employee_1.age)
Output:
Enter name:> John Smith
Enter age:> 40
Employee name: John Smith
Employee age: 40
You do not have to use an input to set the information. You can simply set it from the variable employee_1.
employee_1 = Employee('John Smith', 40)
There is a method using super() where you can create a subclass with extra attributes that wont affect the Parent class.
class Person1(Employee): # passing the parent class through
def __init__(self):
self.email = '[email protected]'
super().__init__(name='John Smith', age=40)
employee = Person1()
employee_1 = Employee('John Doe', 20)
print('Employee name: ', employee.name, '\nEmployee age: ', employee.age, '\nEmployee email: ', employee.email)
print('Employee name: ', employee_1.name, '\nEmployee age: ', employee_1.age)
Output:
Employee name: John Smith
Employee age: 40
Employee email: [email protected]
Employee name: John Doe
Employee age: 20
As you can see I could add the attribute email without the Parent Class being affected however the attributes from the Parent Class were inherited.
firstused?