DEV Community

Discussion on: Reconciling Dataclasses And Properties In Python

Collapse
 
paddyroddy profile image
Patrick Roddy

do you how to combine this with abstract classes? If I do the following:

from dataclasses import dataclass, field
from abc import abstractmethod

@dataclass # type: ignore
class Vehicle:

    wheels: int
    _wheels: int = field(init=False, repr=False)

    @property
    def wheels(self) -> int:
        print('getting wheels')
        return self._wheels

    @wheels.setter
    def wheels(self, wheels: int):
        print('setting wheels to', wheels)
        self._wheels = wheels

    @abstractmethod
    def sound_horn(self):
        raise NotImplementedError

then I get the warning test.py:10: error: Name 'wheels' already defined on line 7