Accessing an instance's __init__ args from outside the class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Eberts

    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:
    [color=blue][color=green][color=darkred]
    >>> someobj = Foo('bar', 'bleck')[/color][/color][/color]
    ('bar', 'bleck')

    Now, I'd like to be able to find out what arguments someobj was called with.
    So first I tried:
    [color=blue][color=green][color=darkred]
    >>> print someobj.args[/color][/color][/color]

    but I get: "AttributeError : args"

    so then I tried:
    [color=blue][color=green][color=darkred]
    >>> print some_obj.__init __.func_default s[/color][/color][/color]

    which returns an empty tuple

    and then I tried:
    [color=blue][color=green][color=darkred]
    >>> some_obj.__dict __['args'][/color][/color][/color]
    Traceback (most recent call last):
    File "<interacti ve 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


  • Duncan Booth

    #2
    Re: Accessing an instance's __init__ args from outside the class

    "Alexander Eberts" <alex_eberts@vi deotron.ca> wrote in
    news:sfAQa.2164 5$O55.673402@wa gner.videotron. net:
    [color=blue]
    > Is there any way to find out what arguments an object was called
    > with?[/color]

    Not in general.
    [color=blue]
    > Are the args stored with the instance?[/color]

    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:
    [color=blue][color=green][color=darkred]
    >>> class Foo:[/color][/color][/color]
    def __init__(self, *args):
    self.args = args
    print args # no problem here

    [color=blue][color=green][color=darkred]
    >>> someobj = Foo('bar', 'bleck')[/color][/color][/color]
    ('bar', 'bleck')[color=blue][color=green][color=darkred]
    >>> someobj.args[/color][/color][/color]
    ('bar', 'bleck')

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

    Comment

    • Alexander Eberts

      #3
      Re: Accessing an instance's __init__ args from outside the class

      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" <duncan@NOSPAMr cp.co.uk> wrote in message
      news:Xns93B8AC7 6381A5duncanrcp couk@127.0.0.1. ..[color=blue]
      > "Alexander Eberts" <alex_eberts@vi deotron.ca> wrote in
      > news:sfAQa.2164 5$O55.673402@wa gner.videotron. net:
      >[color=green]
      > > Is there any way to find out what arguments an object was called
      > > with?[/color]
      >
      > Not in general.
      >[color=green]
      > > Are the args stored with the instance?[/color]
      >
      > 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[/color]
      to[color=blue]
      > do with its arguments. If you create your own class, and want to be able[/color]
      to[color=blue]
      > 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:
      >[color=green][color=darkred]
      > >>> class Foo:[/color][/color]
      > def __init__(self, *args):
      > self.args = args
      > print args # no problem here
      >
      >[color=green][color=darkred]
      > >>> someobj = Foo('bar', 'bleck')[/color][/color]
      > ('bar', 'bleck')[color=green][color=darkred]
      > >>> someobj.args[/color][/color]
      > ('bar', 'bleck')
      >
      > --
      > Duncan Booth duncan@rcp.co.u k
      > int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
      > "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?[/color]


      Comment

      • Ulrich Petri

        #4
        Re: Accessing an instance's __init__ args from outside the class

        "Alexander Eberts" <alex_eberts@vi deotron.ca> schrieb im Newsbeitrag
        news:RKCQa.2775 7$O55.714325@wa gner.videotron. net...[color=blue]
        > Duncan,
        >
        > Thanks for your response - much appreciated. Do you know how the python
        > interpreter handles *args and **kwargs passed to a class's __init__[/color]
        method?[color=blue]
        > (maybe the better question is "what section in the python docs describes[/color]
        how[color=blue]
        > class args are handled" :)
        >[/color]

        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.


        Comment

        Working...