AttributeError:'dict'对象没有属性'charss'

时间:2021-09-05 18:18:52

I am new to OOP with python. I have 3 classes with one being called from the other as a parameter instance:

我是python的OOP新手。我有3个类,其中一个作为参数实例从另一个调用:

class Characters:
    def __init__(self, b="", c = ""):
        self.b= b
        self.c= c           

class Words:
    def __init__(self, charss=Characters()):       
        self.charss= charss

class Sentences:
    def __init__(self, w=Words()):
        self.w = w

I am then creating a new instance of class Words form another Class and filling it with data i have from a json file.

然后我创建一个新的类Words实例形成另一个类,并用json文件中的数据填充它。

s = Sentences()
s.__dict__ = json.loads(output.content)

When debugging i can see that s is populated correctly. I am then trying to print out the values of s like this:

调试时,我可以看到s已正确填充。然后我试图打印出像这样的s的值:

print(s.w[0].charss[0])

an error occurs:AttributeError: 'dict' object has no attribute 'charss'

发生错误:AttributeError:'dict'对象没有属性'charss'

is it because I am populating from the JSON as __dict__ where charss instantiated as list. If that is so what would be the best solution changing the __dict__ from json input or instantiating the object as __dict__?

是因为我从JSON填充为__dict__,其中charss实例化为list。如果是这样的话,那么从json输入改变__dict__或将对象实例化为__dict__的最佳解决方案是什么?

2 个解决方案

#1


0  

When you say s.__dict__ = some_dictionary, you're entirely replacing all the instance variables of s with the ones in some_dictionary. This includes w, which is now defined entirely by whatever's in your JSON. JSON objects are never going to have charss attributes, so it's guaranteed to fail in the way you mention.

当你说s .__ dict__ = some_dictionary时,你完全用some_dictionary中的那些替换s的所有实例变量。这包括w,现在完全由JSON中的任何内容定义。 JSON对象永远不会有charss属性,所以它保证会以你提到的方式失败。

What are you trying to accomplish with s.__dict__ = json.loads(output.content)? Do you just want to store the dictionary in s? If so, s.my_cool_dict_name = json.loads(output.content) might be what you want.

你想用s .__ dict__ = json.loads(output.content)完成什么?你只想把字典存储在s中吗?如果是这样,s.my_cool_dict_name = json.loads(output.content)可能就是你想要的。

#2


0  

The problem is the way the instance s is being accessed for printing. I am saving the json output as a dictionary but the instance of a class is by default a list. therefore instead of

问题是访问实例s进行打印的方式。我将json输出保存为字典,但默认情况下,类的实例是一个列表。因此,而不是

print(s.w[0].charss[0])

that line should be

那条线应该是

print(s.w[0]["charss"][0])

#1


0  

When you say s.__dict__ = some_dictionary, you're entirely replacing all the instance variables of s with the ones in some_dictionary. This includes w, which is now defined entirely by whatever's in your JSON. JSON objects are never going to have charss attributes, so it's guaranteed to fail in the way you mention.

当你说s .__ dict__ = some_dictionary时,你完全用some_dictionary中的那些替换s的所有实例变量。这包括w,现在完全由JSON中的任何内容定义。 JSON对象永远不会有charss属性,所以它保证会以你提到的方式失败。

What are you trying to accomplish with s.__dict__ = json.loads(output.content)? Do you just want to store the dictionary in s? If so, s.my_cool_dict_name = json.loads(output.content) might be what you want.

你想用s .__ dict__ = json.loads(output.content)完成什么?你只想把字典存储在s中吗?如果是这样,s.my_cool_dict_name = json.loads(output.content)可能就是你想要的。

#2


0  

The problem is the way the instance s is being accessed for printing. I am saving the json output as a dictionary but the instance of a class is by default a list. therefore instead of

问题是访问实例s进行打印的方式。我将json输出保存为字典,但默认情况下,类的实例是一个列表。因此,而不是

print(s.w[0].charss[0])

that line should be

那条线应该是

print(s.w[0]["charss"][0])