Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Accessing an instance's __init__ args from outside the class

Reply
Thread Tools

Accessing an instance's __init__ args from outside the class

 
 
Alexander Eberts
Guest
Posts: n/a
 
      07-14-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


 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      07-14-2003
"Alexander Eberts" <(E-Mail Removed)> wrote in
news:sfAQa.21645$(E-Mail Removed):

> 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 http://www.velocityreviews.com/forums/(E-Mail Removed)
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?
 
Reply With Quote
 
 
 
 
Alexander Eberts
Guest
Posts: n/a
 
      07-14-2003
Duncan,

Thanks for your response - much appreciated. Do you know how the python
interpreter handles *args and **kwargs passed to a class's __init__ method?
(maybe the better question is "what section in the python docs describes how
class args are handled"

all the best,

Alex

"Duncan Booth" <(E-Mail Removed)> wrote in message
news:Xns93B8AC76381A5duncanrcpcouk@127.0.0.1...
> "Alexander Eberts" <(E-Mail Removed)> wrote in
> news:sfAQa.21645$(E-Mail Removed):
>
> > 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 (E-Mail Removed)
> 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?



 
Reply With Quote
 
Ulrich Petri
Guest
Posts: n/a
 
      07-15-2003
"Alexander Eberts" <(E-Mail Removed)> schrieb im Newsbeitrag
news:RKCQa.27757$(E-Mail Removed).. .
> Duncan,
>
> Thanks for your response - much appreciated. Do you know how the python
> interpreter handles *args and **kwargs passed to a class's __init__

method?
> (maybe the better question is "what section in the python docs describes

how
> class args are handled"
>


They are handled exactly as they would be in any other method. It seems you
have a bit of a misconception on how the __init__ method works!?

Ciao Ulrich

PS: To post an answer above the aswered text is not exactly liked on the
(use)net.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Should one always add super().__init__() to the __init__? Ramchandra Apte Python 17 09-30-2012 12:04 PM
help with pix inside->outside + dmz->outside + inside->outside->dmz Jack Cisco 0 09-19-2007 01:57 AM
super(...).__init__() vs Base.__init__(self) Kent Johnson Python 7 02-12-2006 08:59 PM
Is there a class or method to construct url args or extract url args? Ken Varn ASP .Net 2 06-22-2005 12:26 PM
__new__ does not call __init__ as described in descrintro.html (WAS:Can __new__ prevent __init__ from being called?) Steven Bethard Python 2 02-16-2005 06:50 AM



Advertisments