PEP 255: Simple Generators

Andrew Dalke dalke at acm.org
Fri Jun 22 17:14:45 EDT 2001


Carsten Geckeler:
>So Test is called like a function and returns an instance.  But why make a
>different syntax ("class") for defining it?  Just because it returns an
>instance instead an integer or another object?  According to your
>argument, "def" would be fine.

There isn't quite enough information to distinguish between at
least some class definitions and function definitions without the
use of a keyword.  Consider

>>> class SuperTest:
... pass
...
>>> def Test(SuperTest):
...     def __init__(self, spam):
...         self.spam = spam
...     def __str__(self):
...         return str(self.spam)
>>> t = Test("eggs")
>>> str(t)
'None'
>>>

Changing the 'def Test' to 'class Test' changes the
entire meaning of the code.

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list