Print the __str__()ΒΆ

Note

It is now easier to view DEBUG-level log messages from this library! Check out the Easier Debug Mode section.

You might want an opt-in __str__ method on classes that inherit from DataclassWizard. This opt-in method will format the dataclass instance as a prettified JSON string, for example whenever str(obj) or print(obj) is called.

If you want to opt in to this __str__ method, you can pass str=True as shown below:

from dataclass_wizard import DataclassWizard


class MyClass(DataclassWizard, str=True):
    my_str: str = 'hello world'
    my_int: int = 2


c = MyClass()
print(c)
# prints:
#   {'my_int': 2, 'my_str': 'hello world'}

Dataclass Wizard

Bring Python dataclasses to life β€” the wizard way!

Useful Links

  • Overview
  • Installation
  • Examples
  • Py Compatibility
  • Common Use Cases
  • Advanced Usage
  • Release History
  • Contributors Guide
  • Env Magic
  • CLI Tool
  • Using Field Properties
  • πŸ—£ GitHub Discussions
  • 🎯 Issue Tracker
  • Dataclass Wizard @
  • GitHub | PyPI | Anaconda | Stack Overflow

Related Topics

  • Documentation overview
    • Common Use Cases
      • Previous: Patterned Date and Time
      • Next: Serialization Options

API Reference

  • Index
  • Module Index

This Page

  • Show Source

Quick search

©2021-2026, Ritvik Nag. | Page source
Fork me on GitHub