'asdict() inside .format() in a Python class
How can I use asdict() method inside .format() in oder to unpack the class attributes.
So that instead of this:
from dataclasses import dataclass, asdict
@dataclass
class InfoMessage():
training_type: str
duration: float
distance: float
message = 'Training type: {}; Duration: {:.3f} ч.; Distance: {:.3f}'
def get_message(self) -> str:
return self.message.format(self.training_type, self.duration, self.distance)
I could write something like this:
@dataclass
class InfoMessage():
training_type: str
duration: float
distance: float
message = 'Training type: {}; Duration: {:.3f} ч.; Distance: {:.3f}'
def get_message(self) -> str:
return self.message.format(asdict().keys())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
