New inited instance of class?

Samuel Kleiner sam at samuel-kleiners-computer.local
Sat Dec 6 23:05:00 EST 2003


Francis Avila wrote:
>
> Samuel Kleiner wrote in message ...
>>Is there a builtin way of making making another instance of your own
>>class? 
>
> You mean, from the inside (from one of the instance methods of the class)?

Yes.

> def new(self, *args, **kargs):
>     return self.__class__(*args, **kargs)

This works. Thanks. 

>>I really expected type(self)(*args, **keywords) to work this way.
>
> Works for me.  What traceback does it give you?

Not for me. I really want to call it as the function itself, and

type(self)(argument1,argument2,argument3)

fails with

Traceback (most recent call last):
  File "<stdin>", line 139, in ?
  File "<stdin>", line 78, in __add__
TypeError: instance() takes at most 2 arguments (3 given)

whereas

self.__class__(argument1,argument2,argument3)

does not

>>Currently i'm doing this:
>>
>>[my code]
> That's ugly.

Yes.

-- 
On an encouraging note, however, I found that throughout the source code,
extremely conservative coding practices and good error checking everywhere
means that our software does not crash when handling IPv6 addresses.
--Joe Loughry, Lockheed Martin Space and Strategic Missiles, RADIANT MERCURY




More information about the Python-list mailing list