I have this code:
class MyClass:
a = None
b = None
now I have this part in my code:
my_class_instance = MyClass(a=3, b=5)
I am getting this error:
TypeError: MyClass() takes no arguments
Is there a way to initialize MyClass
with values for a
and b
without creating a constructor?
One possibility is a dataclass:
from dataclasses import dataclass
@dataclass
class MyClass:
a: int
b: int
mc = MyClass(a=3, b=5)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.