Good morning,
I'm having a problem trying to use __ slots __ in one of my classes. I want to use it to optimize the creation of multiple instances of the Classifier object. I'm using Python 3.7.1.
This Works
class Classifier() :
def __init__(self, own_storage, partner_storage, decision, strength) :
self.own_storage = own_storage
self.partner_storage = partner_storage
self.decision = decision
self.strength = strength
This doesn't work
class Classifier() :
__slots__ = ('own_storage', 'partner_storage', 'decision', 'strength')
def __init__(self, own_storage, partner_storage, decision, strength) :
self.own_storage = own_storage
self.partner_storage = partner_storage
self.decision = decision
self.strength = strength
Here is where I create all the instances of Classifier (It is a method of another class)
def allClassifiers(self) :
cs = []
own_range = []
partner_range = []
for i in range(1, self.agent.model.n_types + 1) :
own_range.append(i)
own_range.append(-i)
partner_range.append(i)
partner_range.append(-i)
for own in own_range :
for partner in partner_range :
for decision in [0, 1] :
cs.append(Classifier(own, partner, decision, i))
return cs
The error I'm getting:
Traceback (most recent call last):
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\test.py", line 53, in <module>
m = Model(10000, 1, 1, init_distribution = [0, 1, 1, 0], state_t = "uniform")
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\model.py", line 53, in __init__
a = Agent(id_, i, self, production_i, h)
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\agent.py", line 31, in __init__
self.cs = ClassifierSystem(self)
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\classifier_system.py", line 17, in __init__
self.classifiers = self.allClassifiers()
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\classifier_system.py", line 41, in allClassifiers
cs.append(Classifier(own, partner, decision, i))
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\classifier.py", line 7, in __init__
self.own_storage = own_storage
TypeError: descriptor 'own_storage' for 'Classifier' objects doesn't apply to 'Classifier' object
I've seen multiple examples on how to use __ slots __ but I can't get where I'm making a mistake. Thank you for your help.
EDIT: If I try this code, it works
class Classifier() :
__slots__ = ('own_storage', 'partner_storage', 'decision', 'strength')
def __init__(self, own_storage, partner_storage, decision, strength) :
self.own_storage = own_storage
self.partner_storage = partner_storage
self.decision = decision
self.strength = strength
c = []
for i in range(0, 70) :
c.append(Classifier(1, 2, 1, i))
Also, if I put the last 3 lines of this code inside the allClassifiers function (third section of code I put in the question), it works.
EDIT 2: There is something I don't get with how Spyder works. Running the program from shell, it works perfectly in any case. With Spyder, after deleting the variables of the previous run, now the slot version works, and the version without it gives me this error:
Traceback (most recent call last):
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\test.py", line 53, in <module>
m = Model(10000, 1, 1, init_distribution = [0, 1, 1, 0], state_t = "uniform")
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\model.py", line 53, in __init__
a = Agent(id_, i, self, production_i, h)
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\agent.py", line 31, in __init__
self.cs = ClassifierSystem(self)
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\classifier_system.py", line 17, in __init__
self.classifiers = self.allClassifiers()
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\classifier_system.py", line 41, in allClassifiers
cs.append(Classifier(own, partner, decision, i))
File "C:\Users\Diru\Desktop\Internship\Sketch\classes\classifier.py", line 7, in __init__
self.own_storage = own_storage
AttributeError: 'Classifier' object has no attribute 'own_storage'
At least now I know that the problem is that I don't know how to use Spyder
I was facing the same issue.
Solution: Restart Spyder.
Hope it solves the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.