Python 3.7 introduces the dataclasses
module that contains a @dataclass
decorator. This decorator can generate class functions. How can I print these generated functions?
The __post_init__ method is called just after initialization. In other words, it is called after the object receives values for its fields, such as name , continent , population , and official_lang .
The dataclass allows you to define classes with less code and more functionality out of the box. This Person class has the __init__ method that initializes the name and age attributes. If you want to have a string representation of the Person object, you need to implement the __str__ or __repr__ method.
This function is a decorator that is used to add generated special methods to classes, as described below. The dataclass() decorator examines the class to find field s. A field is defined as a class variable that has a type annotation.
Dataclasses, introduced in Python 3.7 (and backported to Python 3.6), provide a handy way to make classes less verbose. Many of the common things you do in a class, like instantiating properties from the arguments passed to the class, can be reduced to a few basic instructions.
After inspecting the dataclass implementation, the methods seems to be generated by a dataclasses._create_fn
. To get the original generated code, I mocked the function as:
import dataclasses
_original_create_fn = dataclasses._create_fn
def _new_create_fn(name, args, body, **kwargs):
args_str = ', '.join(args)
body_str = '\n'.join(' ' + l for l in body)
print(f'def {name}({args_str}):\n{body_str}\n')
return _original_create_fn(name, args, body, **kwargs)
dataclasses._create_fn = _new_create_fn
# After the dataclasses as been mocked, creating new dataclass
# will display their source code
@dataclasses.dataclass
class A:
x: int
y: int
Which display something like:
def __init__(self, x:_type_x, y:_type_y):
self.x=x
self.y=y
def __repr__(self):
return self.__class__.__qualname__ + f"(x={self.x!r}, y={self.y!r})"
def __eq__(self, other):
if other.__class__ is self.__class__:
return (self.x,self.y,)==(other.x,other.y,)
return NotImplemented
I asked the same question myself. Dataclasses
part project should have the verbose option, but is not.
Found out this video, helpful. According to the video dataclasses.py is a code generator. So this should be our first idea how to get the code.
I tried this code:
from dataclasses import dataclass
import inspect
import os
from uncompyle6 import PYTHON_VERSION, deparse_code2str
@dataclass
class C:
name: str
value: int = 34
inspect.getmembers(C) #('__init__', <function __init__(self, name: str, value: int = 34) -> None>)
co= C.__init__.__code__ # let's find out code for the __init__ method from code object
code = deparse_code2str(
code=co,
version=PYTHON_VERSION,
out=open(os.devnull, "w"))
print(code)
Will print
self.name = name
self.value = value
The code acutally uses the inspector to understand the class, and then to decompile the methods using Python decompiler.
Here are the methods discovered:
def __eq__(self, other):
if other.__class__ is self.__class__:
return (self.name, self.value) == (
other.name,
other.value,
)
else:
return NotImplemented
def __init__(self, name: str, value: int = 34) -> None:
self.name = name
self.value = value
def __repr__(self):
key = (id(self), _thread.get_ident())
if key in repr_running:
return "..."
else:
repr_running.add(key)
try:
result = user_function(self)
finally:
repr_running.discard(key)
return result
There is actually a project that is doing @dataclass discovery. I installed it and it worked.
from dataclasses import dataclass
import inspect
import os
import dis
from DataclassInspector.inspector import Inspector
@dataclass
class C:
name: str
value: int = 34
inspected = Inspector(C)
print(inspected._generate_code())
Provided the output like this:
from dataclasses import Field, _MISSING_TYPE, _DataclassParams
class C:
__dataclass_fields__ = {'name': "Field(name='name', type=str, default=_MISSING_TYPE, default_factory=_MISSING_TYPE, init=True, repr=True, hash=None, compare=True, metadata={}, _field_type=_FIELD)", 'value': "Field(name='value', type=int, default=34, default_factory=_MISSING_TYPE, init=True, repr=True, hash=None, compare=True, metadata={}, _field_type=_FIELD)"}
__dataclass_params__ = _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False)
name: str
value: int = 34
def __eq__(self, other):
if other.__class__ is self.__class__:
return (self.name, self.value) == (other.name, other.value)
else:
return NotImplemented
__hash__ = None
def __init__(self, name: str, value: int = 34) -> None:
self.name = name
self.value = value
def __repr__(self):
key = (
id(self), _thread.get_ident())
if key in repr_running:
return '...'
else:
repr_running.add(key)
try:
result = user_function(self)
finally:
repr_running.discard(key)
return result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With