Re: Python 2.6 Deprecation Warnings with __new__ — Can someone explain why?

Stephen Hansen apt.shansen at gmail.com
Fri Oct 23 06:09:15 CEST 2009


On Thu, Oct 22, 2009 at 8:12 PM, rh0dium <steven.klass at gmail.com> wrote:

> In my case the args that it dumps them into a black hold is simply not
> true.  I want an unknown set of args and kwargs to simply be forwarded
> onto init.  So what's the problem with this??
>

There is no problem with doing that-- the deprecation warning is just that
object.__new__ takes no arguments besides the class itself.

Within __new__ you do not need to do anything to "forward" the arguments to
__init__. Python calls first __new__, then __init__ with the arguments you
pass. You don't have to forward from one method to another.

Observe:

>>> class myclass(object):
...     def __new__(cls, *args, **kwargs):
...             print args
...             print kwargs
...             self = object.__new__(cls)
...             return self
...     def __init__(self, *args, **kwargs):
...             print args
...             print kwargs
...
>>> A = a()
()
{}
()
{}
>>> A = a(1,2,3)
(1, 2, 3)
{}
(1, 2, 3)
{}

HTH,

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091022/e5a69085/attachment.html>


More information about the Python-list mailing list