Does PyModule_GetDict return information about class method variables?

Discussion in 'Python' started by MD, Aug 10, 2007.

  1. MD

    MD Guest

    Hi,

    I have a variable which is defined inside a class method. When I
    call PyModule_GetDict on the module containing this class, the
    dictionary doesn't contain any information about this variable. Is
    this expected behavior? If so, what options do I have to access this
    variable from my Python C extension.

    Thanks and Regards,
    -MD
    MD, Aug 10, 2007
    #1
    1. Advertising

  2. Re: Does PyModule_GetDict return information about class methodvariables?

    On Thu, 09 Aug 2007 19:34:37 -0700, MD wrote:

    > I have a variable which is defined inside a class method. When I
    > call PyModule_GetDict on the module containing this class, the
    > dictionary doesn't contain any information about this variable. Is
    > this expected behavior? If so, what options do I have to access this
    > variable from my Python C extension.


    You can't access names in methods because they don't exist until you call
    the method. It's just like local variables in C. Consider:

    void foo(void)
    {
    int bar = 42;
    }

    Here `bar` does not exist until you call `foo()` and it disappears as soon
    as the function returns.

    It's the very same situation in Python:

    class A(object):
    def foo(self):
    bar = 42

    The local name `bar` only exists if `foo()` is called on an instance of `A`.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Aug 10, 2007
    #2
    1. Advertising

  3. MD

    MD Guest

    Hi Marc,
    Thanks for your reply. I am calling my extension function from the
    class method itself. So at that point the variable does exist. I am
    puzzled why PyModule_GetDict is not able to access the variable even
    though it does exist at that point.

    Thanks,
    -Manas

    On Aug 10, 12:43 am, Marc 'BlackJack' Rintsch <> wrote:
    > On Thu, 09 Aug 2007 19:34:37 -0700, MD wrote:
    > > I have a variable which is defined inside a class method. When I
    > > call PyModule_GetDict on the module containing this class, the
    > > dictionary doesn't contain any information about this variable. Is
    > > this expected behavior? If so, what options do I have to access this
    > > variable from my Python C extension.

    >
    > You can't access names in methods because they don't exist until you call
    > the method. It's just like local variables in C. Consider:
    >
    > void foo(void)
    > {
    > int bar = 42;
    >
    > }
    >
    > Here `bar` does not exist until you call `foo()` and it disappears as soon
    > as the function returns.
    >
    > It's the very same situation in Python:
    >
    > class A(object):
    > def foo(self):
    > bar = 42
    >
    > The local name `bar` only exists if `foo()` is called on an instance of `A`.
    >
    > Ciao,
    > Marc 'BlackJack' Rintsch
    MD, Aug 10, 2007
    #3
  4. Re: Does PyModule_GetDict return information about class methodvariables?

    On Fri, 10 Aug 2007 05:54:03 -0700, MD wrote:

    > On Aug 10, 12:43 am, Marc 'BlackJack' Rintsch <> wrote:
    >> class A(object):
    >> def foo(self):
    >> bar = 42
    >>
    >> The local name `bar` only exists if `foo()` is called on an instance of `A`.

    >
    > Thanks for your reply. I am calling my extension function from the
    > class method itself. So at that point the variable does exist. I am
    > puzzled why PyModule_GetDict is not able to access the variable even
    > though it does exist at that point.


    It does not exist in the module or the function object but on the stack.
    Let's go to C again:


    void baz(void);

    void foo(void)
    {
    int bar = 42;
    baz();
    }

    How do you get from `baz()` the value of `foo()`\s local `bar`? Other
    than ugly non portable stack trickery!?

    Why don't you just give the object as argument to your C function?
    Wanting to poke around in the callers name space is code smell. Don't do
    that.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Aug 10, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clercmedia
    Replies:
    2
    Views:
    32,250
    clercmedia
    Dec 9, 2005
  2. Greenhorn
    Replies:
    15
    Views:
    797
    Keith Thompson
    Mar 6, 2005
  3. lerenard001
    Replies:
    0
    Views:
    416
    lerenard001
    Jul 7, 2008
  4. lerenard001
    Replies:
    0
    Views:
    459
    lerenard001
    Jul 9, 2008
  5. albert kao
    Replies:
    7
    Views:
    2,960
Loading...

Share This Page