[Tutor] Decorators: Are they good for checking inputs and outputs?

Alan Gauld alan.gauld at btinternet.com
Sun Jan 6 14:44:48 CET 2013


On 06/01/13 12:30, DoanVietTrungAtGmail wrote:

> After much reading and head-scratching, I think the basic idea of
> decorators has now clicked for me.

Congratulations. It's always good when a new concept finally slots in place.

One thing - do you understand the downsides of decorators too?
Every feature in a language has an upside and a downside. You don't 
really understand it until you understand both aspects.

> Python, but I want to eventually develop a serious system. To spend most
> of my time on developing the ideas and building the code, I need to test
> my code fairly well but spend as little time doing so as possible.

See the recent discussion on Test Driven Development.
You can spend your time writing decorators or you can write test 
functions/classes. Either way you do it once. With automated tests
the time spent testing is usually minimal even with many tests. at least 
at the unit test level.

> Specifically, for every function I will write, I don't want to have to
> write code to check that arguments passed to it are of the permitted
> number, type, and range, then code to deal with those errors that can be
> dealt with. This is what I hope: Once I have collected or written all
> the necessary decorators, from then on I'll just routinely decorate each
> function with a whole bunch of decorators relevant to it.

That's probably possible but remember that each decorator is effectively 
another function wrapped around your function. Many decorators means 
many function calls. That may have an impact both on the performance and 
the ease of debugging your code.

> The above is about inputs, but decorators equally seem able to check
> outputs and the function's inner workings: that it returns results which
> are within range, that loops in the function don't go for longer than
> usual, etc.

It can check inputs and outputs. I'm not so sure about the last bit 
though! There might be a way but I can't think of one without some
pretty hairy timer techniques.

> However, it seems not many people think like I do. In the discussions I
> have read on StackOverflow and elsewhere, few people use decorators for
> the above purposes, and those who do, don't seem to do it extensively.

Most people use a TDD framework and write test functions. They ensure 
the code works and don't get in the way of the finished version.

> If decorators are truly as useful as I think they are for the above
> purposes, surely they would be used more enthusiastically, more
> extensively, by many more people.

Decorators are useful for many things but they have their downsides too.
And there is also convention and "standard practice", the culture of 
Python programming, the common idioms. Decorators haven't really made it 
into mainstream python coding idioms yet.

> So, my first question to the tutors is: Am I giving decorators
> undeservedly high expectations for the above purposes (ie. checking
> inputs & outputs, and dealing with some problems therein)? Are there
> perhaps traps down the line, invisible to me at the moment, which make
> decorators not as useful as I hope?

I've never tried what you suggest so I don't know the impacts.
My initial concerns would be over potential performance penalties
and ease of debugging if things do go wrong.

> Second, if decorators are actually suitable for the above purposes, then
> where can I find repositories of decorators? (I am aware of
> the PythonDecoratorLibrary

I wasn't and hadn't thought of such a concept till now.
Thanks for triggering that idea, I will investigate.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list