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

TypeError: object of type 'property' has no len()

I have this code of a class that contains a list _offers. For this list I created a property to allow access to it:

class OffersManager:    
    _offers: list[OfferData] = []
    
    @property
    def offers(cls) -> list[OfferData]:
        return cls._offers

The problem is that the program treats this list as a "property" object, so it gives me an error when I try to get the length of the list:

>>> len(OffersManager.offers)
>>> Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        len(OffersManager.offers)
    TypeError: object of type 'property' has no len()

Can someone tell me the correct way to make a property return the attribute as a list?

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 :

You need to mark the property as a class method, so it’s bound to the class itself rather than the instances:

class OffersManager:    
    _offers: list[OfferData] = []
    
    @classmethod
    @property
    def offers(cls) -> list[OfferData]:
        return cls._offers
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