简体   繁体   中英

extending built-in python dict class

I want to create a class that would extend dict's functionalities. This is my code so far:

class Masks(dict):

    def __init__(self, positive=[], negative=[]):
        self['positive'] = positive
        self['negative'] = negative

I want to have two predefined arguments in the constructor: a list of positive and negative masks. When I execute the following code, I can run

m = Masks()

and a new masks-dictionary object is created - that's fine. But I'd like to be able to create this masks objects just like I can with dicts:

d = dict(one=1, two=2)

But this fails with Masks:

>>> n = Masks(one=1, two=2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'two'

I should call the parent constructor init somewhere in Masks. init probably. I tried it with **kwargs and passing them into the parent constructor, but still - something went wrong. Could someone point me on what should I add here?

You must call the superclass __init__ method. And if you want to be able to use the Masks(one=1, ..) syntax then you have to use **kwargs :

In [1]: class Masks(dict):
   ...:     def __init__(self, positive=(), negative=(), **kwargs):
   ...:         super(Masks, self).__init__(**kwargs)
   ...:         self['positive'] = list(positive)
   ...:         self['negative'] = list(negative)
   ...:         

In [2]: m = Masks(one=1, two=2)

In [3]: m['one']
Out[3]: 1

A general note: do not subclass built-ins!!! It seems an easy way to extend them but it has a lot of pitfalls that will bite you at some point.

A safer way to extend a built-in is to use delegation, which gives better control on the subclass behaviour and can avoid many pitfalls of inheriting the built-ins. (Note that implementing __getattr__ it's possible to avoid reimplementing explicitly many methods)

Inheritance should be used as a last resort when you want to pass the object into some code that does explicit isinstance checks.

Since all you want is a regular dict with predefined entries, you can use a factory function.

def mask(*args, **kw):
    """Create mask dict using the same signature as dict(),
    defaulting 'positive' and 'negative' to empty lists.
    """
    d = dict(*args, **kw)
    d.setdefault('positive', [])
    d.setdefault('negative', [])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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