File: skip_the_str.rst

package info (click to toggle)
dataclass-wizard 0.35.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,808 kB
  • sloc: python: 15,276; makefile: 111; javascript: 23
file content (34 lines) | stat: -rw-r--r-- 929 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Skip the :meth:`__str__`
========================

.. note::
    It is now easier to view ``DEBUG``-level log messages from this library! Check out
    the `Easier Debug Mode <easier_debug_mode.html>`__ section.

The ``JSONSerializable`` class implements a default
``__str__`` method if a sub-class doesn't already define
this method. This 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 out of this default ``__str__`` method,
you can pass ``str=False`` as shown below:


.. code:: python3

    from dataclasses import dataclass

    from dataclass_wizard import JSONSerializable


    @dataclass
    class MyClass(JSONSerializable, str=False):
        my_str: str = 'hello world'
        my_int: int = 2


    c = MyClass()
    print(c)
    # prints the same as `repr(c)`:
    #   MyClass(my_str='hello world', my_int=2)