python-dataclasses

Data Classes vs typing.NamedTuple primary use cases

微笑、不失礼 提交于 2020-02-17 08:57:22
问题 Long story short PEP-557 introduced data classes into Python standard library, that basically can fill the same role as collections.namedtuple and typing.NamedTuple . And now I'm wondering how to separate the use cases in which namedtuple is still a better solution. Data classes advantages over NamedTuple Of course, all the credit goes to dataclass if we need: mutable objects inheritance support property decorators, manageable attributes generated method definitions out of the box or

Data Classes vs typing.NamedTuple primary use cases

痴心易碎 提交于 2020-02-17 08:56:14
问题 Long story short PEP-557 introduced data classes into Python standard library, that basically can fill the same role as collections.namedtuple and typing.NamedTuple . And now I'm wondering how to separate the use cases in which namedtuple is still a better solution. Data classes advantages over NamedTuple Of course, all the credit goes to dataclass if we need: mutable objects inheritance support property decorators, manageable attributes generated method definitions out of the box or

How to use the __post_init__ method in Dataclasses in Python

痞子三分冷 提交于 2020-01-25 08:15:00
问题 I am trying to get my hands dirty with dataclasses in Python and what i want to do is have a computed field inside my class and also add the sort_index field to the call but would also want to make it frozen so that i cannot modify any attributes of this class after definition. Below is my code: from dataclasses import dataclass, field def _get_year_of_birth(age: int, current_year: int=2019): return current_year - age @dataclass(order=True, frozen=True) class Person(): sort_index: int = field

Using queue.PriorityQueue, not caring about comparisons

﹥>﹥吖頭↗ 提交于 2020-01-25 03:04:07
问题 I'm trying to use queue.PriorityQueue in Python 3(.6). I would like to store objects with a given priority. But if two objects have the same priority, I don't mind PriorityQueue.get to return either. In other words, my objects can't be compared at integers, it won't make sense to allow them to be, I just care about the priority. In Python 3.7's documentation, there's a solution involving dataclasses . And I quote: If the data elements are not comparable, the data can be wrapped in a class

How do I document a constructor for a class using Python dataclasses?

南笙酒味 提交于 2020-01-24 02:09:13
问题 I have some existing Python 3.6 code that I'd like to move to Python 3.7 dataclasses. I have __init__ methods with nice docstring documentation, specifying the attributes the constructors take and their types. However, if I change these classes to use the new Python dataclasses in 3.7, the constructor is implicit. How do I provide constructor documentation in this case? I like the idea of dataclasses, but not if I have to forego clear documentation to use them. edited to clarify I'm using

dataclasses.Field doesn't resolve type annotation to actual type

人走茶凉 提交于 2020-01-16 14:34:12
问题 The documentation for the Field class of python's standard dataclasses module specifies only: Its documented attributes are: [...] type: The type of the field. To me, it seems to mean that the field will contain the type itself, and not only it's name in the form of a string. However, it seems that it simply copies the type annotation as is, making it quite useless. Example: @dataclasses.dataclass class C: c: 'C' dataclasses.fields(C)[0].type # This returns the string 'C' typing.get_type

How to document callable class with Sphinx?

房东的猫 提交于 2020-01-16 09:39:29
问题 I have a callable class with @dataclass annotation. I want to document it the way, so in the documentation, people can distinguish difference between __call__ and __init__ methods. Since I am using @dataclass __init__ is automatically generated. Example of the code in my case: @final @dataclass(frozen=True, slots=True) class Casting(object): """Cast one type of code to another. Constructor arguments: :param int_converter_function: function to convert to int :param float_converter_function:

Is it possible to use *args in a dataclass?

走远了吗. 提交于 2020-01-14 07:05:35
问题 I recently started using dataclasses and they will be a nice addition to 3.7. I'm curious if or how it is possible to recreate the same functionality of this class using dataclasses. class Nav(object): def __init__(self, name:str, menu, page, *submenus): self.name = name self.menu = menu self.page = page self.submenus = submenus foo = Nav("name", "menu", "page") This doesn't work. Raises Exception TypeError: __init__() missing 1 required positional argument: 'submenus' @dataclass class Nav

Pythonic way to check if a dataclass field has a default value

℡╲_俬逩灬. 提交于 2020-01-03 08:46:07
问题 I've been using python 3.7 lately and was looking for ways to leverage the new dataclasses. Basically I had a method that iterates over the dataclass fields and checks if they have a default value: from dataclasses import fields, MISSING @classmethod def from_json(cls) datacls_fields = fields(cls) for field in datacls_fields: if (field.default != MISSING): #... However in the official documentation, it says: MISSING value is a sentinel object used to detect if the default and default_factory

Force type conversion in python dataclass __init__ method

我与影子孤独终老i 提交于 2020-01-02 02:52:07
问题 I have the following very simple dataclass: import dataclasses @dataclasses.dataclass class Test: value: int I create an instance of the class but instead of an integer I use a string: >>> test = Test('1') >>> type(test.value) <class 'str'> What I actually want is a forced conversion to the datatype i defined in the class defintion: >>> test = Test('1') >>> type(test.value) <class 'int'> Do I have to write the __init__ method manually or is there a simple way to achieve this? 回答1: The type