How to change behavior of dict() for an instance

前端 未结 6 1264
悲哀的现实
悲哀的现实 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:19

    Once you have your dictify function working just do

    dict = dictify
    

    Update: Here is a short way to have this recursive dict:

    >>> def RecursiveDict():
    ...   return defaultdict(RecursiveDict)
    

    Then you can:

    d[1][2][3] = 5
    d[1][2][4] = 6
    >>> d
    defaultdict(, {1: defaultdict(, {2: defaultdict(, {3: 5, 4: 6})})})
    

    I don't see a neat way to implement dictify.

提交回复
热议问题