PEP 255: Simple Generators

Carsten Geckeler uioziaremwpl at spammotel.com
Fri Jun 22 18:03:33 EDT 2001


On Fri, 22 Jun 2001, Andrew Dalke wrote:

> 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.

Don't take my `proposal' to change "class" into "def" too serious.
Defining classes was thought as a counter-example for using "def" for
generators.  The point of some posters was, that because calling a
generator returns a object, is a reason to justify why "def" is
appropriate for defining a generator.

I just wanted to point out that "calling a class" (i.e.  creating an
instance by calling class(args)) also returns an object, but that this is
of course not a reason to define it with "def", because the way the
definition is evaluated and what is returned is completly different as how
a function definition is evaluated (as you pointed out).  And the same
with generators.

Cheers, Carsten
-- 
Carsten Geckeler





More information about the Python-list mailing list