[ python-Bugs-1155938 ] yield in __init__ causes broken new-style class

SourceForge.net noreply at sourceforge.net
Thu Mar 3 17:56:44 CET 2005


Bugs item #1155938, was opened at 2005-03-03 10:42
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1155938&group_id=5470

Category: Type/class unification
Group: Python 2.4
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Steve Alexander (stevea_zope)
Assigned to: Raymond Hettinger (rhettinger)
Summary: yield in __init__ causes broken new-style class

Initial Comment:

 class Foo(object):
 
     def __init__(self):
         print "foo"
         raise RuntimeError
         yield 23

 Foo()

With a classic class, this correctly fails saying
"__init__ must return None".  With a new-style class,
the class suite works, and Foo() is called without
reporting an error, but also without printing "foo".


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2005-03-03 11:56

Message:
Logged In: YES 
user_id=80475

Fixed.
See typeobject.c 2.264 and 2.264.2.1.

----------------------------------------------------------------------

Comment By: Steve Alexander (stevea_zope)
Date: 2005-03-03 11:46

Message:
Logged In: YES 
user_id=492001

Albertas Agejevas has pointed out that:

1. New style classes don't care what is returned from __init__.

2. If __init__ contains a yield, the code in __init__ won't
be called until the generator that is returned has its
'.next()' method called.

Which all makes sense.  However, someone I work with was
trying for ages to work out what a particular __init__
method was not being called.  The __init__ method was rather
long, and there was a yield, copy-pasted from some other
code, hidden behind a raise.

So, the behaviour may be correct, but I don't think it is
very helpful. 

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2005-03-03 11:02

Message:
Logged In: YES 
user_id=6656

You don't have to be that cute:

>>> class Foo(object):
...  def __init__(self):
...   return "foo"
... 

where did that check go?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1155938&group_id=5470


More information about the Python-bugs-list mailing list