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

DoanVietTrungAtGmail doanviettrung at gmail.com
Sun Jan 6 13:30:14 CET 2013


Dear tutors

After much reading and head-scratching, I think the basic idea of
decorators has now clicked for me. I am a beginner in programming and in
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. Therefore,
I am looking ahead and thinking of using decorators extensively.

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.

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.

The above is about functions. As to classes and methods, I am still
learning about object-oriented programming, but I'd think decorators can be
just as useful.

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. 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.

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?

Second, if decorators are actually suitable for the above purposes, then
where can I find repositories of decorators? (I am aware of
the PythonDecoratorLibrary - PythonInfo Wiki, the link is not working now,
but did a few days ago)

Thank you!

Trung Doan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130106/a40c1922/attachment.html>


More information about the Tutor mailing list