python-dataclasses

Class inheritance in Python 3.7 dataclasses

强颜欢笑 提交于 2019-11-28 20:14:58
I'm currently trying my hands on the new dataclass constructions introduced in Python 3.7. I am currently stuck on trying to do some inheritance of a parent class. It looks like the order of the arguments are botched by my current approach such that the bool parameter in the child class is passed before the other parameters. This is causing a type error. from dataclasses import dataclass @dataclass class Parent: name: str age: int ugly: bool = False def print_name(self): print(self.name) def print_age(self): print(self.age) def print_id(self): print(f'The Name is {self.name} and {self.name} is

Type hints in namedtuple

穿精又带淫゛_ 提交于 2019-11-27 19:33:58
Consider following piece of code: from collections import namedtuple point = namedtuple("Point", ("x:int", "y:int")) The Code above is just a way to demonstrate as to what I am trying to achieve. I would like to make namedtuple with type hints. Do you know any elegant way how to achieve result as intended? Wolfgang Kuehn The prefered Syntax for a typed named tuple since 3.6 is from typing import NamedTuple class Point(NamedTuple): x: int y: int = 1 # Set default value Point(3) # -> Point(x=3, y=1) Edit Starting Python 3.7, consider using dataclasses (your IDE may not yet support them for

Validating detailed types in python dataclasses

♀尐吖头ヾ 提交于 2019-11-27 12:52:19
Python 3.7 is around the corner , and I wanted to test some of the fancy new dataclass +typing features. Getting hints to work right is easy enough, with both native types and those from the typing module: >>> import dataclasses >>> import typing as ty >>> ... @dataclasses.dataclass ... class Structure: ... a_str: str ... a_str_list: ty.List[str] ... >>> my_struct = Structure(a_str='test', a_str_list=['t', 'e', 's', 't']) >>> my_struct.a_str_list[0]. # IDE suggests all the string methods :) But one other thing that I wanted to try was forcing the type hints as conditions during runtime, i.e.

What are data classes and how are they different from common classes?

╄→гoц情女王★ 提交于 2019-11-27 11:00:56
With PEP 557 data classes are introduced into python standard library. They make use of the @dataclass decorator and they are supposed to be "mutable namedtuples with default" but I'm not really sure I understand what this actually means and how they are different from common classes. What exactly are python data classes and when is it best to use them? Data classes are just regular classes that are geared towards storing state, more than contain a lot of logic. Every time you create a class that mostly consists of attributes you made a data class. What the dataclasses module does is make it

Class inheritance in Python 3.7 dataclasses

纵饮孤独 提交于 2019-11-27 10:19:11
问题 I'm currently trying my hands on the new dataclass constructions introduced in Python 3.7. I am currently stuck on trying to do some inheritance of a parent class. It looks like the order of the arguments are botched by my current approach such that the bool parameter in the child class is passed before the other parameters. This is causing a type error. from dataclasses import dataclass @dataclass class Parent: name: str age: int ugly: bool = False def print_name(self): print(self.name) def

Creating nested dataclass objects in Python

江枫思渺然 提交于 2019-11-27 03:32:05
问题 I have a dataclass object that has nested dataclass objects in it. However, when I create the main object, the nested objects turn into a dictionary: @dataclass class One: f_one: int @dataclass class One: f_one: int f_two: str @dataclass class Two: f_three: str f_four: One data = {'f_three': 'three', 'f_four': {'f_one': 1, 'f_two': 'two'}} two = Two(**data) two Two(f_three='three', f_four={'f_one': 1, 'f_two': 'two'}) obj = {'f_three': 'three', 'f_four': One(**{'f_one': 1, 'f_two': 'two'})}

Type hints in namedtuple

无人久伴 提交于 2019-11-26 19:47:22
问题 Consider following piece of code: from collections import namedtuple point = namedtuple("Point", ("x:int", "y:int")) The Code above is just a way to demonstrate as to what I am trying to achieve. I would like to make namedtuple with type hints. Do you know any elegant way how to achieve result as intended? 回答1: The prefered Syntax for a typed named tuple since 3.6 is from typing import NamedTuple class Point(NamedTuple): x: int y: int = 1 # Set default value Point(3) # -> Point(x=3, y=1) Edit

Validating detailed types in python dataclasses

烈酒焚心 提交于 2019-11-26 11:04:32
问题 Python 3.7 is around the corner, and I wanted to test some of the fancy new dataclass +typing features. Getting hints to work right is easy enough, with both native types and those from the typing module: >>> import dataclasses >>> import typing as ty >>> ... @dataclasses.dataclass ... class Structure: ... a_str: str ... a_str_list: ty.List[str] ... >>> my_struct = Structure(a_str=\'test\', a_str_list=[\'t\', \'e\', \'s\', \'t\']) >>> my_struct.a_str_list[0]. # IDE suggests all the string