[Python-3000] Generic function PEP won't make it in time
Christian Heimes
lists at cheimes.de
Fri Apr 27 19:28:00 CEST 2007
Phillip J. Eby wrote:
> As someone with more recent background in Java than C++, I find the idea of
> abstract methods having an executable implementation to be quite confusing,
> and suspect that other people with that Java or C# background will do the
> same thing. That is, skim the explanation and miss the significant
> difference between the C++ way and what they're used to.
From the naive point of view from somebody with less background
knowledge about ABCs, abstract methods and generics I expect that
* an abstract class can't be instantiated. An abstract class must
be sub-classed before it can be instantiated.
* an abstract method has no implementation and simply raises a
NonImplementedError based exception.
* a class with abstract methods is automatically an abstract class
unless generics are entering the game of class creation. When a
generic overwrites an abstract method the class is a concrete
class but when the abstract method is called an exception is
raised.
class AbstractEgg(abstract=True):
"""Abstract class, cannot be instantiated
"""
def egg(self, foo):
pass
or:
@abstract
class AbstractEgg:
pass
class Egg(AbstractEgg):
"""Subclass can be instantiated
"""
class AbstractSpam:
"""Abstract class because it contains an abstract method
"""
@abstract
def egg(self, foo):
pass
class Spam(AbstractSpam):
"""Still an abstract method because it doesn't overwrite egg()
"""
class GenericSpam(AbstractSpam):
"""Subclass can be instantiated
I'm not sure about the @generic syntax but it's just an example
"""
@generic(int)
def egg(self, foo):
pass
>>> gs = GenericSpam()
This works:
>>> gs.egg(1)
This raises an AbstractMethodException(NotImplementedException)
>>> gs.egg('foo')
Just my 5 cents from an ordinary Python user who is trying to learn ABCs
and generics.
Christian
More information about the Python-3000
mailing list