简体   繁体   English

为什么我不能在列表中的对象上调用 __dict__ ,而该对象恰好在字典中的列表中的另一个对象中?

[英]Why can't I call __dict__ on object in list that happens to be within another object within a list within a dictionary?

Here's my setup: dictD contains a key users paired with value = list of UserObjects .这是我的设置: dictD包含与 value = list of UserObjects配对的关键users Each UserObject has an attribute username plus two arrays, threads and comments.每个UserObject都有一个属性 username 加上两个数组、线程和注释。

I was able to convert dictD's array of user objects into a dictionary style with this call:我能够通过这个调用将 dictD 的用户对象数组转换为字典样式:

dictD["users"] = [user.__dict__ for user in dictD["users"] ] dictD["users"] = [user.__dict__ for user in dictD["users"] ]

If I dump out dictD, here's the relevant part before I try to do my manipulation:如果我放弃 dictD,那么在我尝试进行操作之前,这是相关部分:

{
    'users':[
    {
        'username': Redditor(user_name='$$$$$$$$$$'),
        'threads':[
            <__main__.redditThread instance at 0x7f05db28b320>
        ],
    'comments':[
        <__main__.comment instance at 0x7f05db278e60>
    ]
},
{
    'username': Redditor(user_name='##########e\ gone'),
    'threads':[
        <__main__.redditThread instance at 0x7f05db2a4a70>
    ],
    'comments':[
        <__main__.comment instance at 0x7f05db298e18>
    ]
}

As you can see the comments contain comment objects and the threads list contains thread objects.如您所见,注释包含comment对象,线程列表包含thread对象。 So I'd like to do the same call for them that I did for the users array.所以我想为他们做与我为users数组所做的相同的调用。 But when I try to do this:但是当我尝试这样做时:

for user in dictD["users"]:
    user.threads = [thread.__dict__ for thread in user.threads]
    user.comments = [comment.__dict__ for comment in user.comments]

I run into this error:我遇到了这个错误:

AttributeError: 'dict' object has no attribute 'threads'

I also tried我也试过

users = dictD["users"]
for user in users...

but this triggers the same error message.但这会触发相同的错误消息。 How can I turn objects in lists into dictionary form when those objects' lists are themselves held within objects within lists within a dictionary?当这些对象的列表本身保存在字典中的列表中的对象中时,如何将列表中的对象转换为字典形式?

Incidentally, I am doing all this so I can insert these objects into MongoDB, so if there is an easier way to serialize a complex object, please let me into the secret.顺便说一下,我做这一切是为了可以将这些对象插入到 MongoDB 中,所以如果有更简单的方法来序列化复杂的对象,请让我进入秘密。 Thank you.谢谢你。

Promoting my comment to an answer since it seems reasonable and nobody else is posting: it looks at a glance like you're confusing Python for Javascript: a dict with a key 'threads' is not an object you can reference with .threads , only with ["threads"] .将我的评论提升为一个答案,因为它似乎是合理的,而且没有其他人在发帖:它乍一看就像您将 Python 与 Javascript 混淆:带有键“线程”的 dict 不是您可以使用.threads引用的对象,只有与["threads"] ie. IE。 user.threads should be user["threads"] . user.threads应该是user["threads"] A dict usually only has the same standard attributes (see: https://docs.python.org/2/library/stdtypes.html#typesmapping or https://docs.python.org/3/library/stdtypes.html#mapping-types-dict for Python 3.) The problem isn't that you're trying to call __dict__ on an object, it's that you're trying to get an attribute from an object that doesn't exist, later in that same line of code. dict 通常只有相同的标准属性(参见: https : //docs.python.org/2/library/stdtypes.html#typesmappinghttps://docs.python.org/3/library/stdtypes.html#用于 Python 3 的mapping-types-dict 。)问题不在于您试图在对象上调用__dict__ ,而是您试图从不存在的对象中获取属性,稍后在相同的情况下代码行。

If you want to recreate complex objects from MongoDB rather than just nested dicts and lists then that is basically a process of deserialization;如果您想从 MongoDB 重新创建复杂对象,而不仅仅是嵌套的字典和列表,那么这基本上是一个反序列化过程; you can either handle that manually, or maybe use some sort of object mapping library to do it for you (eg. something like Mongoobject might work, though I've not tested it myself)您可以手动处理,也可以使用某种对象映射库来为您处理(例如Mongoobject 之类的东西可能会起作用,尽管我自己没有测试过)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM