PEP 3107 and stronger typing (note: probably a newbie question)
George Sakkis
george.sakkis at gmail.com
Wed Jun 20 19:41:24 EDT 2007
On Jun 20, 5:22 pm, Stephen R Laniel <s... at laniels.org> wrote:
> Perhaps it would help for me to explain what I'd like.
>
> Under both Perl and Python, I've found myself
> having/wanting to write things like so:
>
> def my_func( int_arg, str_arg ):
> try:
> int_arg = int( int_arg )
> str_arg = str( str_arg )
> except ValueError:
> sys.stderr.write( "Args are not of the right type\n" )
> sys.exit(1)
After several hundreds of such checks, good luck making the 2.0
version of your application Unicode-friendly.
> Granted, in a dynamic language we won't always (maybe "won't
> often") have a situation where the types are known this well
> at compile time. But sometimes we will. And it would be nice
> to catch these before the program even runs.
Optional typing has its merits but your example is more of a
counterexample, an example where the explicit try/catch doesn't really
buy you anything. You'd get essentially the same effect if you just
attempted the conversion and let any exception propagate:
def my_func(some_non_hungarian_notation_meaningful_name,
other_non_hungarian_notation_meaningful_name):
a = int(some_non_hungarian_notation_meaningful_name)
b = str(other_non_hungarian_notation_meaningful_name)
More often than not the types that you *think* are required will turn
out to be more (and sometimes less) restrictive than necessary. Still,
If you're addicted to manifest typing [1], the typechecking module [2]
may give you a warm and fuzzy feeling:
from typecheck import accepts
@accepts(int, str)
my_func(some_non_hungarian_notation_meaningful_name,
other_non_hungarian_notation_meaningful_name):
HTH,
George
[1] http://c2.com/cgi/wiki?ManifestTyping
[2] http://oakwinter.com/code/typecheck/tutorial/basics.html
More information about the Python-list
mailing list