How to change behavior of dict() for an instance

前端 未结 6 1267
悲哀的现实
悲哀的现实 2021-01-30 09:21

So I\'m writing a class that extends a dictionary which right now uses a method \"dictify\" to transform itself into a dict. What I would like to do instead though is change it

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 10:06

    You can't do it.

    I deleted my previous answer, because I found after looking at the source code, that if you call dict(d) on a d that is a subclass of dict, it makes a fast copy of the underlying hash in C, and returns a new dict object.

    Sorry.

    If you really want this behavior, you'll need to create a RecursiveDict class that doesn't inherit from dict, and implement the __iter__ interface.

提交回复
热议问题