Just good advice? = WAS("Re: getting system date")

Terry Reedy tjreedy at udel.edu
Sun May 11 15:12:28 EDT 2003


"David Broadwell" <anti-spam.dbroadwell at mindspring.com> wrote in
message news:jwlva.5957$TM6.2501 at nwrddc02.gnilink.net...
>
> "Terry Reedy" <tjreedy at udel.edu> wrote in message
> news:S0CdnRakruvh3yCjXTWcpw at comcast.com...
> > I know this was answered for you, but to be successful with
Python,
> > learn to use the interpreter interactively and especially the
> > type(obj) builtin function.  'print type(x)' is also an important
> > debugging tool in pre-written scripts.
>
> Can I take that to mean that type errors are a major pitfall to many
> programmers

Depends on the programmer ;-) -- but less so for people who use type()
to get the info they need before falling into a hole.

> and that I should be learning to avoid them?

Of course ;-)

> If so: Suggestions?

More explanation: I have noticed over a period of time that several
questions asked on the list could have been answered more quickly by
the original poster if the OP had known how to use type().  A few
questions, such as the one I was directly responding to, have been as
trivial as 'what type of object does this function return' (in
particular, the OP wrote "what type is that result?").

More subtle was a posting a week ago.  The poster reported code
something like:
...
x = func1(*args)
func2(x,*args)
...
and an exception trace that started with func2 and ended about 10
layers deep in the library with an error message about getting a tuple
instead of an int.  If the OP had known to insert 'print type(x)'
before the func2() call, he would have answered his own question.  If
before writing the code, he had interactively tried
'type(func1(*args))' to refresh his faulty memory about func1's return
value, there never would have been a bug to ask about.

One can also use the manual (which I also do), but actual evidence is
more definitive and possibly faster.  Even when I am writing in an
editor, I sometimes use an interactive window for quick answers about
actual behavior.

Hope this helps a bit.

Terry J. Reedy






More information about the Python-list mailing list