newbie-one more example of difficulty in van Rossum's tutorial

Brian van den Broek broek at cc.umanitoba.ca
Wed Dec 14 07:02:25 CET 2005


bobueland at yahoo.com said unto the world upon 2005-12-13 15:44:
> This is from 9.6 (Private variables). I quote
> 
> - "Notice that code passed to exec, eval() or evalfile() does not
> consider the classname of the invoking class to be the current class;
> this is similar to the effect of the global statement, the effect of
> which is likewise restricted to code that is byte-compiled together.
> The same restriction applies to getattr(), setattr() and delattr(), as
> well as when referencing __dict__ directly."
> 
> I've read the text so far but there has been no explaination of exec,
> eval() and evalfile() so far. I suspect they give ways for dynamic
> execution of text strings containing Python statements.
> But what does it mean that "code passed to exec, eval() or evalfile()
> does not consider the classname of the invoking class to be the current
> class"? What "invoking class"? What "current class".
> 
> Now there's a hint:
> 
> - "this is similar to the effect of the global statement, the effect of
> which is likewise restricted to code that is byte-compiled together."
> 
> But what does this mean? So far global statement was only mention on
> page 64 by the sentence
> 
> - "If a name is declared global, then all references and assignments go
> directly to the middle scope containing the module's global names."
> 
> What has this to do with
> 
> - " the effect of which is likewise restricted to code that is
> byte-compiled together."
> 
> At last I learn that
> 
> - "The same restriction applies to getattr(), setattr() and delattr(),
> as well as when referencing __dict__ directly."
> 
> But  getattr(), setattr() and delattr() and  __dict__ has not been
> mentioned as far as I recollect.
> 
> Since no examples are given I don't know what to do with this and what
> the intention was putting the text above in the tutorial.. Of course I
> can go to library reference and try to uncover the details there but
> it's not an easy job if you are a newbie (which is why I started with
> the tutorial to begin with).
> 
> Well, I don't want to complain to much about a tutorial which is rather
> good but would appreciate some hint or reference which would help me to
> understand the quoted text.
> 
> Bob

Hi Bob,

again, I'm no guru, so look to others for proper explanations. That 
said, perhaps this code will help you see what is going on:

class A(object):
     def __init__(self):
         self.b = 1
         self.__b = 42
     def get__b(self):
         return self.__b
     def evalget(self, arg):
         return eval("self.%s" %arg)

a=A()
print a.get__b()
print a.evalget('b')
print a.evalget('__b')

When run, this produces:

42
1

Traceback (most recent call last):
   File "C:/Python24/foobar.py", line 13, in -toplevel-
     print a.evalget('__b')
   File "C:/Python24/foobar.py", line 8, in evalget
     return eval("self.%s" %arg)
   File "<string>", line 0, in -toplevel-
AttributeError: 'A' object has no attribute '__b'
 >>>

In fuzzy terms, a method of a class doesn't have to mangle its __ 
names to access them. But, access through eval, etc. does require the 
mangled reference; it is as if the eval doesn't know it is being 
invoked in the class being accessed.

HTH,

Brian vdB




More information about the Python-list mailing list