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

AttributeError: 'int' object has no attribute item

The code is:

Offers = [0, 13, 4]
class Shop:
    def __init__(self, item, price, count):
        self.item = item
        self.price = price
        self.count = count

    def CreateNew(self):
        Offers.append(self.item)

Shop.CreateNew(3)

Why does it happen? I’ve been wasting hours searching for an solution, no result.

The error occurs at:

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

Offers.append(self.item)

>Solution :

Are you thinking something like this?

Offers = [0, 13, 4]
class Shop:
    def __init__(self, item, price, count):
        self.item = item
        self.price = price
        self.count = count

    def CreateNew(self):
        Offers.append(self.item)

# Create new shop object
# Giving None for your price and count, since you don't have them in your example
s = Shop(3, None, None) 
# Call objects method
s.CreateNew()

Or if you want to use CreateNew as a class method you can call without creating a new object, you can do it like this

Offers = [0, 13, 4]
class Shop:
    def __init__(self, item, price=None, count=None):
        self.item = item
        self.price = price
        self.count = count
    
    @classmethod
    def CreateNew(cls, item, price, count):
        c = cls(item, price, count)
        Offers.append(c.items)
        return c


# This adds item to Offers list and returs new shop class for you. 
Shop.CreateNew(3)

But using class methods (or static methods) is unusual in Python. And perhaps a bit advanced in this context. This approach is more common in for example C#.

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