Is there a clever way to pass the key to defaultdict's default_factory?

Posted by Paul Oyster on Stack Overflow See other posts from Stack Overflow or by Paul Oyster
Published on 2010-05-26T10:55:14Z Indexed on 2010/05/26 11:31 UTC
Read the original article Hit count: 163

Filed under:

A class has a constructor which takes one parameter:

class C(object):
    def __init__(self, v):
        self.v = v
        ...

Somewhere in the code, it is useful for values in a dict to know their keys.
I want to use a defaultdict with the key passed to newborn default values:

d = defaultdict(lambda : C(here_i_wish_the_key_to_be))

Any suggestions?

© Stack Overflow or respective owner

Related posts about python