[Python-3000] adding @abstractmethods after class creation (original) (raw)

Calvin Spealman ironfroggy at gmail.com
Wed Apr 25 04:42:26 CEST 2007


On 4/24/07, Steven Bethard <steven.bethard at gmail.com> wrote:

On 4/24/07, guido.van.rossum <python-checkins at python.org> wrote: > +We define a new built-in decorator, @abstractmethod [snip] > +Implementation: The @abstractmethod decorator sets the > +function attribute _isabstractmethod_ to the value True. > +The type._new_ method computes the type attribute > +_abstractmethods_ as the set of all method names that have an > +_isabstractmethod_ attribute whose value is true. It does this > +by combining the _abstractmethods_` attributes of the base_ _> +classes, adding the names of all methods in the new class dict that_ _> +have a true _isabstractmethod_ attribute, and removing the names_ _> +of all methods in the new class dict that don't have a true_ _> +_isabstractmethod_ attribute. If the resulting_ _> +_abstractmethods_set is non-empty, the class is considered_ _> +abstract, and attempts to instantiate it will raiseTypeError``.

(Hope this wasn't covered in that really long thread. I couldn't see an answer in the PEP.) So what happens in a situation like:: class C: pass C.foo = abstractmethod(foo) It seems from the description above like C._abstractmethods_ would be empty, and therefore C could be instantiated even though it has an abstract method. I don't mind at all if we just say "don't do that", but then @abstractmethod should probably be documented explicitly as only being usable within a class body. (Note that this is different from @classmethod and @staticmethod which can be used after the fact.) I guess the other option would be to have setattr on classes append to the abstractmethods list when passed a value with isabstractmethod == True. But this would probably make setting attributes on classes (though not on other objects) slower. STeVe -- I'm not in-sane. Indeed, I am so far out of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy


Python-3000 mailing list Python-3000 at python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/ironfroggy%40gmail.com

Or could abstractmethods be created dynamically? Which would be used less, abstractmethods or setting class attributes, that it would be OK to be a bit slower?

-- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/



More information about the Python-3000 mailing list