Accessing an instance's __init__ args from outside the class

Alexander Eberts alex_eberts at videotron.ca
Mon Jul 14 11:46:38 EDT 2003


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):
        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

but I get: "AttributeError: args"

so then I tried:

>>> print some_obj.__init__.func_defaults

which returns an empty tuple

and then I tried:

>>> some_obj.__dict__['args']
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
KeyError: args

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
there are no answers (that I could see) to this question. Any help would be
much appreciated.

yours,

Alex Eberts






More information about the Python-list mailing list