I'm using the csv.DictWriter
class, and I want to inherit it:
class MyObj(csv.Dictwriter):
...
But this type is an old-style object. Can MyObj
be a new-style class but still inherit from csv.DictWriter
?
I'm using the csv.DictWriter
class, and I want to inherit it:
class MyObj(csv.Dictwriter):
...
But this type is an old-style object. Can MyObj
be a new-style class but still inherit from csv.DictWriter
?
Yes, you only have to inherit from object
, too:
class MyObj(object, csv.DictWriter):
def __init__(self, f, *args, **kw):
csv.DictWriter.__init__(self, f, *args, **kw)
As Daniel correctly states, you need to mixin object
. However, one major point of using new-style classes is also using super
, thus you should use
class MyObj(csv.DictWriter, object):
def __init__(self, csvfile, mycustomargs, *args, **kwargs):
super(MyOobj, self).__init__(csvfile, *args, **kwargs)
...
As mentioned elsewhere, object
should be the last parent, otherwise object
's default methods such as __str__
and __repr__
will override the other parent's implementation, which is certainly not what you wanted...