End of file

Bengt Richter bokr at oz.net
Mon Oct 11 04:29:31 EDT 2004


On Mon, 11 Oct 2004 15:58:44 +1100, Andrew Durdin <adurdin at gmail.com> wrote:

>On Mon, 11 Oct 2004 04:51:28 GMT, Bengt Richter <bokr at oz.net> wrote:
>> why is class apparently not legal as a simple statement terminated by ';' ?
>> (I wanted to attempt an alternate one-liner ;-)
>> 
>>  >>> class Record:pass; rec=Record()
>>  ...
>>  Traceback (most recent call last):
>>    File "<stdin>", line 1, in ?
>>    File "<stdin>", line 1, in Record
>>  NameError: name 'Record' is not defined
>
>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



More information about the Python-list mailing list