Typing arguments in python
Steven Taschuk
staschuk at telusplanet.net
Wed Apr 16 14:19:15 EDT 2003
More information about the Python-list mailing list
Wed Apr 16 14:19:15 EDT 2003
- Previous message (by thread): Typing arguments in python
- Next message (by thread): Typing arguments in python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Quoth Danra:
[...]
> The advantages of the second definition are obvious - The fact that
> the argument passed doesn't have the write method would be detected
> immediately, rather than when trying to call the it.
>
> Why is that so important? [...]
> 1) The stuff you do before calling write takes a lot of time, which
> could be saved if the error is detected when the function is called.
This is a really puzzling comment. What kind of time are you
saving? Running time of the program? You can hardly have
discovered through profiling that the case which needs to be
optimized is the one in which your function is called with a
nonwritable object.
> 2) The object gets passed around from function to function, and maybe
> changes its name on the way. In this case it would be more difficult
> to trace the problem from the errorneous call to the original function
> call.
*boggle* Read the traceback. Use a debugger. Look again at the
code you just changed.
...
Your line of thinking leads to things like this:
def foo(writable f):
# ...
bar(f)
# ...
def bar(writable f):
# ...
f.write('spam')
# ...
Here foo makes no use of f except to pass it to bar; bar needs f
to have a .write method. For "early detection of problems", you
would, I surmise, want to have foo's argument declared as
writable, as above. (After all, if only bar declares it writable,
a call to foo with a nonwritable object won't fail until bar is
called.)
Then you change bar to use a different method of f, and it is no
longer required to be writable. Now you have to go find all calls
to bar and make sure that the functions making those calls don't
needlessly require their arguments to be writable.
This is tight coupling in action, and a fancy way to lose the
advantages of dynamic typing.
--
Steven Taschuk "The world will end if you get this wrong."
staschuk at telusplanet.net -- "Typesetting Mathematics -- User's Guide",
Brian Kernighan and Lorrinda Cherry
- Previous message (by thread): Typing arguments in python
- Next message (by thread): Typing arguments in python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list