Specifing arguments type for a function

Mike Duffy Mike.S.Duffy at gmail.com
Wed Jun 21 05:25:04 EDT 2006


Paolo Pantaleo wrote:
> I have a function
>
> def f(the_arg):
> ...
>
> and I want to state that the_arg must be only of a certain type
> (actually a list). Is there a way to do that?

I wrote a cool function decorator just for that purpose. It's posted on
the Python Decorator Library at:

http://wiki.python.org/moin/PythonDecoratorLibrary#head-308f2b3507ca91800def19d813348f78db34303e


If you use it, you will be able to simply write:

@accepts(list)
def f(the_arg):
    ...

and you will get a warning message if the_arg is not a list. Or, if you
want an exception raised, then just:

@accepts(list, debug=2)
def f(the_arg):
    ...

Let me know what you think, if you do end up using it. I'm eager for
feedback.




More information about the Python-list mailing list