Creating instances of untrusted new-style classes

greg greg at cosc.canterbury.ac.nz
Fri May 26 10:33:03 CEST 2006


Michael Spencer wrote:

>  >>> class A(object):
> ...
>  >>> b = object.__new__(A)

Note that you'll need to be a bit cleverer if the
class might be derived from some other built-in
type:

 >>> class A(list):
...  pass
...
 >>> b = object.__new__(A)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: object.__new__(A) is not safe, use list.__new__()
 >>> b = list.__new__(A)
 >>> b
[]

I'm not sure what is the easiest way to figure out
what base class to use, though. One way would be
to work your way backwards along the __mro__ until
one of them succeeds, but there's probably a more
direct way.

--
Greg



More information about the Python-list mailing list