End of file

B

Bengt Richter

This is the equivalent of:

class Record:
pass
rec = Record()
D'oh ;-/
Not thinking.
That is, the whole line after the : is interpreted as the body of the
class. The name Record is not defined within its body, hence the
NameError.

Another [sort-of related] question: why does the following not produce
a NameError for "foo"?

def foo(): print foo
foo()
the foo in the print line isn't looked for until foo is executing,
by which time foo is bound. In contrast to the body of the class definition,
which executes right away to create the contents of the class dict etc.,
so there the class name is not yet bound.

Regards,
Bengt Richter
 
A

Andrew Durdin

The name of a function is bound to the function object when the def statement
is executed. However, the function's code block is not executed until f is
called, at which point the name f has already been bound to the function
object and is thus available from the globals.

What I wasn't expecting was that "foo" would automatically be looked
up in the enclosing scope... for some reason I'd forgotten about
nested scopes, I don't know why.
 
G

Greg Ewing

Bengt said:
I originally thought that too, but (is this a 2.3.2 bug?):

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'object' object has no attribute 'x'

I stand corrected! I had forgotten that instances of the
bare object class don't have a __dict__.

So it appears that sentinels are about the only practical
use for them.
 

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

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top