7

Here is the my code in python 3.6

class A(object)

    def __init__(self, a: str):
        self._int_a: int = int(a)  # desired composition

    def get_int_a(self) -> int:
        return self._int_a   

I want to rewrite this code in python 3.7, how can i initialize self._int_a: int = int(a) with dataclasses module?

I know that i can do something like that but i can't get how to initialize _a: int = int(a) or similar to that.

from dataclasses import dataclass


@dataclass
class A(object):
    _a: int = int(a)  # i want to get initialized `int` object here

def get_int_a(self) -> int:
    return self._a

Thanks in advance for your ideas and suggestions.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
Vladimir Yahello
  • 243
  • 1
  • 2
  • 10

1 Answers1

18

Do away with getters and setters entirely and just use attribute access. We can define an init only field that accepts a string and then convert that string to an integer field in our __post_init__ call.

from dataclasses import dataclass, InitVar, field

@dataclass
class A:
    temp: InitVar[str]
    a: int = field(init=False)
    def __post_init__(self, temp):
        self.a = int(temp)

x = A("1")
print(type(x.a), x.a)
# <class 'int'> 1
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • What I can't understand with the mechanism is that in the end, `temp` remains as a field for the final object... The dataclass mechanism should remove it completely from the fields (as is done when called the module-level `fields` method, but not done in `__dataclass_fields__`). – Cedric H. Jul 31 '20 at 14:43
  • 2
    I'm not sure I understand what you're saying. `x.temp` should lead to an AttributeError – Patrick Haugh Aug 01 '20 at 16:14
  • 1
    Indeed, I was confused, it leads to an AttributeError as expected. – Cedric H. Aug 02 '20 at 15:00
  • OK, now I see what was the issue: imagine the same example but now give a default value to temp in the class definition. Now you don't have an AttributeError, which is weird. – Cedric H. Aug 02 '20 at 21:43
  • ok but this breaks keyword init – Jules G.M. Feb 24 '22 at 18:21
  • @JulesG.M. not sure what you mean, can you give an example? – Patrick Haugh Feb 24 '22 at 19:25
  • `A(a=1234)` won't work, right? because a isn't in the `__init__` method, `temp` is. So if you're initializing from a `**dict`, your out of luck – Jules G.M. Feb 27 '22 at 17:38
  • was trying to get something like https://stackoverflow.com/questions/53376099/python-dataclass-from-a-nested-dict – Jules G.M. Feb 27 '22 at 17:43