简体   繁体   English

对象的描述符不适用于 object,使用 __slots__

[英]descriptor for objects doesn't apply to object, using __slots__

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.我想用它来优化分类器 object 的多个实例的创建。 I'm using Python 3.7.1.我正在使用 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)这是我创建 Classifier 的所有实例的地方(它是另一个类的方法)

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.此外,如果我将此代码的最后 3 行放在 allClassifiers function (我在问题中的第三部分代码)中,它就可以工作。

EDIT 2: There is something I don't get with how Spyder works.编辑 2: Spyder 的工作方式有一些我不明白的地方。 Running the program from shell, it works perfectly in any case.从 shell 运行程序,它在任何情况下都能完美运行。 With Spyder, after deleting the variables of the previous run, now the slot version works, and the version without it gives me this error:使用 Spyder,在删除上一次运行的变量后,现在插槽版本可以工作,而没有它的版本给了我这个错误:

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至少现在我知道问题是我不知道如何使用 Spyder

I was facing the same issue.我面临着同样的问题。
Solution: Restart Spyder.解决方案:重新启动 Spyder。
Hope it solves the issue.希望它能解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象的描述符“__dict__”不适用于使用 type() 的 object - descriptor '__dict__' for objects doesn't apply to object using type() 将文档字符串添加到__slots__描述符? - Adding docstring to __slots__ descriptor? 类型错误:“dict”对象的描述符“值”不适用于“类型”对象 - TypeError: descriptor 'values' for 'dict' objects doesn't apply to a 'type' object 类型错误:“str”对象的描述符“编码”不适用于“元组”object - TypeError: descriptor 'encode' for 'str' objects doesn't apply to a 'tuple' object TypeError:“list”对象的描述符“append”不适用于“float” object - TypeError: descriptor 'append' for 'list' objects doesn't apply to a 'float' object 如果一个对象没有`__dict__`,它的类必须有`__slots__`属性吗? - If an object doesn't have `__dict__`, must its class have a `__slots__` attribute? 使用 __slots__ 有什么缺点吗? - Are there disadvantages of using __slots__? python __slots__ 描述符 getter/setter - python __slots__ descriptor getter/setter Python __slots__不适用于某些类声明 - Python __slots__ doesn't work with a certain class declaration TypeError: 'datetime.datetime' 对象的描述符 'date' 不适用于 'int' object - TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM