[Python-ideas] Decorator to avoid a mistake

Steven D'Aprano steve at pearwood.info
Fri Nov 25 19:32:58 EST 2016


On Fri, Nov 25, 2016 at 02:21:28PM -0500, Nick Timkovich wrote:
> You can do it at run-time, if you so desire, without a measurable
> performance hit with a metaclass. Here's a hacky demo:
> https://gist.github.com/nicktimko/5f08d6adfa1dbe1319c3bfc715ec0aa4#file-override_guard-ipynb

All I get at that page is "Sorry, something went wrong."

Don't you love informative error messages?

 
> (Pedants: Any performance hit will be constant-time and probably less than
> a stray import that you don't need. If you're worried about optimizing
> constant-time things, I think you have larger problems.)

time.sleep(360000) is constant time *wink*

Sorry I couldn't resist... 

But seriously... this sort of check belongs in a linter, not in the core 
language, because its not necessarily an error or a mistake to override 
an existing method. In subclasses, the ability to create a method with 
the same name as one in a parent class is fundamental to how inheritence 
works, and even within a single class there's a use (admittedly 
uncommon) for re-using the same name:

class Spam:
    def method(self):
        ...

    if condition:
        method = wrapper(method)


The down-side of this flexibility and power is that more responsibility 
is placed in the hands of the programmer, which is hard on beginners. 
Sometimes I think Python-as-a-teaching-language and Python-as-a- 
production-language are strongly opposed. I wonder whether there might 
be a case to be made for a --with-training-wheels option? But that's 
better placed in the IDE, not the core language.

I think this request is perhaps better suited as a feature request for 
IDLE rather than a language feature.


-- 
Steve


More information about the Python-ideas mailing list