I would like to reduce the number of rows in class definition and create __init __ attributes that have same name as __init __ arguments. Is this possible?
class Num():
def __init__(self, arg1, arg2):
self.arg3 = 'three'
# some magic here
a = Num('one', 'two') # instance
then
print(a.arg1) # one
print(a.arg2) # two
print(a.arg3) # three
Numisobject, whose__init__method doesn't take any (non-self) parameters. Hence the error you didn't include in your post. I'm not entirely sure what you mean by the rest of the question.dataclasses? When you say "inherit" do you mean inherit from another class?dataclassesare exactly what you are looking for. They generate an__init__for you so you only need to specify the types of the arguments. You can also just addarg3: str = 'three'in the body to set your third arg.__init__for its own sake. If you feel too many lines are taken up assigning arguments to attributes, that's possibly a sign that your class needs to be redesigned rather than needing to reduce boilerplate.