The code is:
JavaScript
x
12
12
1
Offers = [0, 13, 4]
2
class Shop:
3
def __init__(self, item, price, count):
4
self.item = item
5
self.price = price
6
self.count = count
7
8
def CreateNew(self):
9
Offers.append(self.item)
10
11
Shop.CreateNew(3)
12
Why does it happen? I’ve been wasting hours searching for an solution, no result.
The error occurs at:
JavaScript
1
2
1
Offers.append(self.item)
2
Advertisement
Answer
Are you thinking something like this?
JavaScript
1
16
16
1
Offers = [0, 13, 4]
2
class Shop:
3
def __init__(self, item, price, count):
4
self.item = item
5
self.price = price
6
self.count = count
7
8
def CreateNew(self):
9
Offers.append(self.item)
10
11
# Create new shop object
12
# Giving None for your price and count, since you don't have them in your example
13
s = Shop(3, None, None)
14
# Call objects method
15
s.CreateNew()
16
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
JavaScript
1
18
18
1
Offers = [0, 13, 4]
2
class Shop:
3
def __init__(self, item, price=None, count=None):
4
self.item = item
5
self.price = price
6
self.count = count
7
8
@classmethod
9
def CreateNew(cls, item, price, count):
10
c = cls(item, price, count)
11
Offers.append(c.items)
12
return c
13
14
15
# This adds item to Offers list and returs new shop class for you.
16
Shop.CreateNew(3)
17
18
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#.