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

B

bobueland

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
 
B

Brian van den Broek

(e-mail address removed) 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)

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
 
B

bobueland

Thanks Brian, now I get it. BTW there is no fuzzuness in your
explanaition it is crystal clear.

Bob
 
S

Steve Holden

Thanks Brian, now I get it. BTW there is no fuzzuness in your
explanaition it is crystal clear.
You might also want to note that name-mangling and the reason for it are
pretty advanced topics for an introductory tutorial. Having got thus
far, further learning might better be gained by using Python on actual
projects.

regards
Steve
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top