473,387 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Accessing an instance's __init__ args from outside the class

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
Jul 18 '05 #1
3 2356
"Alexander Eberts" <al*********@videotron.ca> wrote in
news:sf********************@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 du****@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?
Jul 18 '05 #2
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" <du****@NOSPAMrcp.co.uk> wrote in message
news:Xn***************************@127.0.0.1...
"Alexander Eberts" <al*********@videotron.ca> wrote in
news:sf********************@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 du****@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?

Jul 18 '05 #3
"Alexander Eberts" <al*********@videotron.ca> schrieb im Newsbeitrag
news:RK********************@wagner.videotron.net.. .
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.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Ferrell | last post by:
I have a question about assigning __call__ to an instance to make that instance callable. I know there has been quite a bit of discussion about this, and I've read all I can find, but I'm still...
14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
2
by: Phil | last post by:
I am using a Pascal like language (Wealth-Lab) on W2K and call this server: class HelloWorld: _reg_clsid_ = "{4E797C6A-5969-402F-8101-9C95453CF8F6}" _reg_desc_ = "Python Test COM Server"...
0
by: Mark E. Fenner | last post by:
In the code below, the class DifferentCache utilizes three different memoization (caching) strategies. Neither the function Memoize1 or the class Memoize2 will be adequate for all three of these...
4
by: Donnal Walter | last post by:
This is a question about Python patterns or idioms. Over a period of time, I have evolved a pattern of usage that seems to work better for me than other ways I tried previously, but in writing some...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
11
by: Ryan Krauss | last post by:
I have a set of Python classes that represent elements in a structural model for vibration modeling (sort of like FEA). Some of the parameters of the model are initially unknown and I do some...
7
by: lialie | last post by:
Hi, suppose i have a free_object list. when create a new object sample(*args, **kwds), if free_object_list isn't empty, just pop one from free_object_list instead of creating a new instance. ...
1
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Hi, I was trying to find a way to set, upon __init__() the parent of a class to an existing instance. Here is a minimal example of what I'm trying to do: class A(object): def __init__(self, x):...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.