<div><span class="gmail_quote">On 4/25/07, <b class="gmail_sendername">Guido van Rossum</b> &lt;<a href="mailto:guido@python.org">guido@python.org</a>&gt; wrote:</span></div>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">I&#39;m confused. Can you show a simple example of something that can be<br>checked at definition time? </blockquote>

<div>&nbsp;</div>
<div>Sure. Below is a slightly modified&nbsp;example from the doctest:</div>
<div>&nbsp;</div>
<div>&gt;&gt;&gt; class AbstractCar(AbstractBaseClass): # inherit from AbstractBaseClass to make something abstract<br>...&nbsp;&nbsp;&nbsp;&nbsp; @Abstract<br>...&nbsp;&nbsp;&nbsp;&nbsp; def Drive(self,x): pass</div>
<div>&nbsp;</div>
<div>To make a class abstract, you inherit from AbstractBaseClass in order to get the metaclass set properly and then use a function decorator to declare methods abstract. This is similar to the proposed implementation in your 
abc.py and equally pythonic or unpythonic. The following shows what happens when you define a class that doesn&#39;t override the abstract methods:</div>
<div>&nbsp;</div>
<div>&gt;&gt;&gt; try: # illustrate what happens when you don&#39;t implement @Abstract methods<br>...&nbsp;&nbsp;&nbsp;&nbsp; class fails(AbstractCar): # an erroneous implementation of AbstractCar<br>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>... except AssertionError, e:&nbsp; # doctest: +ELLIPSIS
<br>...&nbsp;&nbsp;&nbsp;&nbsp; print e<br>Class &lt;class &#39;...fails&#39;&gt; must override&nbsp;Drive to implement:<br>[&lt;class &#39;...AbstractCar&#39;&gt;].<br>&nbsp;</div>
<div>The exception gets raised at definition time because the __call__ method of&nbsp;the metaclass inherited from AbstractBaseClass checks whether abstract methods have been implemented.&nbsp;In your implementation, you use the __new__ method of the metaclass to do the check at instantiation. If desired you could enable/disable checking at definition and instantiation separately. 
</div>
<div>&nbsp;</div>
<div>A slightly tricky point is that if something inherits DIRECTLY from AbstractBaseClass (e.g., AbstractCar) then it is allowed to have abstract methods that have not been overridden. This allows you to define a partial abstraction as shown below:
</div>
<div>&nbsp;</div>
<div>&gt;&gt;&gt; class AbstractFastCar(AbstractCar,AbstractBaseClass):# inherit from ABC<br>...&nbsp;&nbsp;&nbsp;&nbsp; @Abstract&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # so that interpreter<br>...&nbsp;&nbsp;&nbsp;&nbsp; def DriveFast(self): pass&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # doesn&#39;t worry about
<br>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Drive being abstract<br>&nbsp;</div>
<div>My implementation will consider this a valid definition because AbstractFastCar inherits DIRECTLY from AbstractBaseClass and is therefore allowed to have abstract methods that have not been overridden. In an earlier post you claimed that my implementation forces you to declare a class abstract redundantly (and therefore unpythonic-ly)&nbsp;when this should be inferred from the class having abstract methods. I believe you were referring to the&nbsp;partially abstract class example above. I agree that this is redundant and unpythonic, but&nbsp;I believe this could be fixed by having the metaclass __call__ method check whether the class itself defines any abstract methods so you wouldn&#39;t need partial abstractions which define abstract methods to still inherit from AbstractBaseClass directly.
</div>
<div>&nbsp;</div>
<div>So far I believe that the main reasonable objection raised to definition time checks is in how the following example would be handled:</div>
<div>&nbsp;</div>
<div>&gt;&gt;&gt; # Define a class that provides a helper function but is still</div>
<div>&gt;&gt;&gt; # abstract in the sense that children must implement Drive.</div>
<div>&gt;&gt;&gt; class AbstractCarHelper(AbstractCar,AbstractBaseClass): </div>
<div>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def DriveHelper(self):</div>
<div>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &#39;Vroom!&#39;</div>
<div>&nbsp;</div>
<div>The example above has no abstract methods, but if you want to make this kind of partial abstraction a legal definition you need to tell the interpreter that this is still an abstract class by inheriting directly from AbstractBaseClass. If we want def time checking, I don&#39;t see a way to make the above definition legal without somehow telling the interpreter to allow it to be abstract. I don&#39;t think this is unpythonic since inheriting from AbstractBaseClass is non-redundant. In fact, one could argue that it is good since it lets the programmer that AbstractCarHelper is still an abstract class even though it does not directly define any abstract methods.
</div>
<div>&nbsp;</div>
<div>If you ignore the last example, I don&#39;t think having def time checks for ABC enforcement has any disadvantages. You can easily turn off the def time checking or have it off by default and turn it on if desired. Since def time checks are done by __call__ and instantiation checks are done by __new__, this shouldn&#39;t interfere with anything in the original ABC PEP. So in my mind, the only reason to object to def time checks is if it bothers you that the last example must inherit directly from AbstractBaseClass. 
</div>
<div><br>&nbsp;</div>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Using the machinery currently in the PEP,<br>any class that inherits an abstract method and doesn&#39;t override it is
<br>automatically an abstract class itself. There&#39;s nothing to complain<br>about at definition time.</blockquote>
<div>&nbsp;</div>
<div>Yes, I see that. What I&#39;m proposing is to&nbsp;have&nbsp;__call__ in the metaclass complain if a class does not inherit directly from AbstractBaseClass and does not have any abstract methods.&nbsp;</div>
<div>&nbsp;</div>
<div>From your point of view, if you have def time checks turned off by default then the difference in using my proposal vs. the original ABC proposal&nbsp;all comes from the last example. For example, your Iterator class in 
abc.py inherits from Iterable (which is abstract) and declares its own abstract method. Under my current implementation this would raise an error unless Iterator also inherits from AbstractBaseClass. This is easily fixable by having my implementation allow classes which declare their own abstract methods to be abstract. The incompatibly I pointed out in my last example is illustrated by the Set class in your 
abc.py. I believe that you want Set to be an ABC which requires children to support the ABC requirements of its parents while providing useful implementations of __le__, __lt__, and __eq__. In my implementation you would need to have Set either&nbsp;inherit directly from AbstractBaseClass or define at least one abstract method to prevent an error at def time (if def time checks are on at all). Note that I don&#39;t think it would be unpythonic to have Set inherit from AbstractBaseClass since it provides someone reading the source code with the useful information that this is an ABC and not a concrete implementation.
</div>
<div>&nbsp;</div>
<div>In summary, let me try to clarify what I see as the only incompatibility between the current PEP and an ABC implementation: with def time checks turned on you would need partial abstractions like Set to somehow indicate that they are abstract by either inheriting directly from AbstractBaseClass or defining at least one abstract method. If people don&#39;t like that, then I don&#39;t have any response. But if people are worried about B&amp;D and other things, I think those are non-issues. B&amp;D at runtime is no better than B&amp;D at def time (I think B&amp;D at runtime is worse).
</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid"><br>Your example implementation was a bit too much code for me to read.<br>:-)&nbsp; </blockquote>
<div>&nbsp;</div>
<div>No problem. Unfortunately, I think I might have made the example usage in this post a little too long to read (but hopefully not). :)</div>
<div>&nbsp;</div>
<div>-Emin</div>