Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

kernel dies after @property in python

I am running the following code in Jupyter:

from typing import List

class dummy:
    def __init__(self, dum: List =[]):
        self.dum = dum

    @property
    def dum(self) -> List:
        return self.dum

    @dum.setter
    def dum(self, value: List) -> None:
        self.dum = value

When I run the following:

dummy(dum=[1,2,3])

The kernel dies without telling me much about the possible error. What is going on?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

@property
def dum(self) -> List:
    return self.dum

This creates an infinite recursion.

  • The attribute itself should have a different name. As per convention, it is usually the name of the property prefixed with an underscore.

  • While we are at it, default mutable arguments should be avoided.

  • mypy does not like implicit Optional annotations, so List = None should be Optional[List] = None.

from typing import List, Optional

class dummy:
    def __init__(self, dum: Optional[List] = None):
        self.dum = dum if dum is not None else []

    @property
    def dum(self) -> List:
        return self._dum

    @dum.setter
    def dum(self, value: List) -> None:
        self._dum = value
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading