initializing mutable class attributes
Alex Martelli
aleaxit at yahoo.com
Thu Sep 2 07:04:08 EDT 2004
Dan Perl <dperl at rogers.com> wrote:
> I found a small hole in the initial code suggestion and I fixed it. Try and
> find the difference:
> class test(object):
> def __new__(typ, *args, **kwargs):
> obj = object.__new__(typ)
> obj.attr1 = 666
> typ.__init__(obj, *args, **kwargs)
> return obj
>
> class derived(test):
> def __init__(self, arg1, arg2):
> self.attr2 = arg1
> self.attr3 = arg2
>
> d = derived(111, 222)
> print d.attr1, d.attr2
> print isinstance(d, test)
>
> So, what do people think about it? Alex, would this be good for a recipe in
> the Python Cookbook?
I might run it side by side with my custom metaclass, sure. I'm not
sure why you're running typ.__init__ from within test.__new__ -- it
seems to me that will result in double-running cases.
Alex
More information about the Python-list
mailing list