End of file
Andrew Durdin
adurdin at gmail.com
Mon Oct 11 00:58:44 EDT 2004
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()
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()
More information about the Python-list
mailing list