Skip to content
Advertisement

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:

JavaScript

I could write something like this:

JavaScript

Advertisement

Answer

asdict should be called on an instance of a class – self, in this case. Additionally, you don’t need the dict’s keys, you need its values, which you can spread using the * operator:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement