Accessing an instance's __init__ args from outside the class

Bengt Richter bokr at
Mon Jul 14 22:24:57 CEST 2003

On Mon, 14 Jul 2003 11:46:38 -0400, "Alexander Eberts" <alex_eberts at> wrote:

>I'm new to python so appologies to the group if this question is asked
>often. I'm wondering if it's possible to query an object instance to find
>out what arguments the instance's __init__ function was called with from
>*outside* the class's local namespace. For example, if I define a class Foo
>as follows:
>import sys
>class Foo:
>    def __init__(self, *args):
         self.args = args                  #[1] save args tuple as attribute of instance (self)
>        print args                        # no problem here
>...and then create an instance of Foo:
>>>> someobj = Foo('bar', 'bleck')
>('bar', 'bleck')
>Now, I'd like to be able to find out what arguments someobj was called with.
>So first I tried:
>>>> print someobj.args
This would work if you saved the args in the __init__ method, like [1]
>but I get: "AttributeError: args"
>so then I tried:
>>>> print some_obj.__init__.func_defaults
               ^--?? this is out of a different test sequence, presumably
>which returns an empty tuple
You would have gotten something if your __init__ had had some parameters with default
values specified (BTW, notice that the 'bar' argument got used for x, and the rest went to *args).

 >>> class Foo:
 ...     def __init__(self, x='x default', *args):
 ...         self.args = args
 ...         self.x = x
 ...         print x, args
 >>> someobj = Foo('bar','bleck','fnord')
 bar ('bleck', 'fnord')
 >>> someobj.x
 >>> someobj.args
 ('bleck', 'fnord')
 >>> print someobj.__init__.func_defaults
 ('x default',)
 >>> someobj.__dict__['args']
 ('bleck', 'fnord')

>and then I tried:
>>>> some_obj.__dict__['args']
This is closely related to some_obj.args, but the latter form will look in the class __dict__ too
and in base class __dict__'s, so e.g., the class could have a default value shared by all instances
that don't shadow it by having instance attributes of the same name.

>Traceback (most recent call last):
>  File "<interactive input>", line 1, in ?
>KeyError: args

You get several attaboys for trying stuff interactively and posting by copy/paste from actual stuff ;-)

>No dice.. Is there any way to find out what arguments an object was called
>with? Are the args stored with the instance? I scoured the python faq but
They aren't stored automatically (though you could inherit from a base class
that did it for you, so it would appear automatic, but never mind that for now ;-)

The ususal thing in __init__ is to save all the things you want the instance to
carry as named attributes, e.g., self.args. You can also set up stuff that wasn't
passed as args, e.g., self.flavors = ['strawberry', lemon', 'lime'], or self.count=0,
etc. They show up later as some_obj.flavors, some_obj.count, etc. (using your instance name).

>there are no answers (that I could see) to this question. Any help would be
>much appreciated.

Bengt Richter

More information about the Python-list mailing list