Accessing an instance's __init__ args from outside the class

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Jul 14 12:08:08 EDT 2003


"Alexander Eberts" <alex_eberts at videotron.ca> wrote in
news:sfAQa.21645$O55.673402 at wagner.videotron.net: 

>  Is there any way to find out what arguments an object was called
> with?

Not in general.

> Are the args stored with the instance?

It depends on the object type. Some objects may save some or all of the 
arguments to the constructor, but it is up to each object to decide what to 
do with its arguments. If you create your own class, and want to be able to 
refer to the __init__ arguments after returning from __init__, then you 
must save the arguments in the object.

So, for your original example you could do:

>>> class Foo:
    def __init__(self, *args):
        self.args = args
        print args                        # no problem here

        
>>> someobj = Foo('bar', 'bleck')
('bar', 'bleck')
>>> someobj.args
('bar', 'bleck')

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list