Note, you are creating instance attributes (via __init__
). You seem to want to change on a copy of the class, not the instance.
Option 1 - A simple approach is to make a new class. (NotImplemented)
Option 2 - Another approach is to subclass a base class.
class Student:
def __init__(self, first, last, age=18, year=2020, major=None):
self.first = first
self.last = last
self.age = age
self.year = year
self.major = major
class MusicStudent(Student):
def __init__(self, *args, year=2023, major="music", **kwargs):
super().__init__(*args, year=2023, major="music", **kwargs)
Demo
s = Student("John", "Doe")
s.first, s.year, s.major
# ('John', 2020, None)
ms = MusicStudent("Jane", "Doe")
ms.first, ms.year, ms.major
# ('Jane', 2023, 'music')
Option 3 - Another option is to make a class factory:
def student_factory(age, year, major):
"""Return a custom Student class."""
class Student:
def __init__(self, first, last, age=age, year=year, major=major):
self.first = first
self.last = last
self.age = age
self.year = year
self.major = major
return Student
Demo
UndeclaredStudent = student_factory(18, 2020, None)
MusicStudent = student_factory(20, 2022, "music")
us = Student("John", "Doe")
us.first, us.age, us.year, us.major
# ('John', 18, 2020, None)
ms = MusicStudent("Jane", "Doe")
ms.first, ms.age, ms.year, ms.major
# ('Jane', 20, 2022, 'music')
There might be other options, but you may need to expound on exactly what you want, perhaps with more examples.