Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Does PyModule_GetDict return information about class method variables?

Reply
Thread Tools

Does PyModule_GetDict return information about class method variables?

 
 
MD
Guest
Posts: n/a
 
      08-10-2007
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

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      08-10-2007
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
 
Reply With Quote
 
 
 
 
MD
Guest
Posts: n/a
 
      08-10-2007
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 <(E-Mail Removed)> 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



 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      08-10-2007
On Fri, 10 Aug 2007 05:54:03 -0700, MD wrote:

> On Aug 10, 12:43 am, Marc 'BlackJack' Rintsch <(E-Mail Removed)> 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
 
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
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
Carriage Return added during return of large string from class method Xeno Campanoli Ruby 0 02-13-2006 08:39 PM
signer information does not match signer information of other classes in the same package clercmedia Java 2 12-09-2005 03:14 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments