Testing if a variable is an integer or a string; exceptions in __del__

Tim Peters tim_one at email.msn.com
Sun May 2 19:57:11 EDT 1999


[Ben Gertzfield]
> ...
> What's the best way to test if a variable is numeric or a string?
> ...
> The genre can either be an integer from 0 to 254 or a string. As
> it is, I'm using
>
> if re.match('^\d+$', options['genre']):
>
> to test if the option is an integer or not. Is this the best way to do
> it?

It's *a* way to do it.  Does something about it bother you?  If you're just
seeking variety, e.g. here's another way

try:
    int(thing)
    lookslikeanint = 1
except:
    lookslikeanint = 0

That's much easier to extend if you want to ensure that the integer value
truly is "from 0 to 254".

> ...
> I'm running Python 1.5.1; it seems that in these later versions, if an
> exception is thrown in an object's __del__ method, a warning is
> printed to STDOUT. However, this warning is not terribly helpful.
> ...
> Exception exceptions.AttributeError: <exceptions.AttributeError
> instance at 80cd930> in <method Id3.__del__ of Id3 instance at
> 80cd738> ignored

"exception ignored" msgs are unique to exceptions raised at system shutdown
time, when the order of module content destruction can't magically ensure
that whatever the heck you happen to reference in your __del__ methods still
exists at the time __del__ is called.  Since the system is shutting down, it
has no choice but to ignore these exceptions and carry on regardless.

If you're not getting this msg at shutdown time, it's a bug and you should
post a minimal failing example.

If you are getting this msg at shutdown time, the cruel-but-fair short
answer is "tough":  the system *is* shutting down, and is severely limited
in what it can do in response to exceptions given that huge portions of the
runtime have gone missing <wink>.

> ...
> When this module is done, I'd love to submit it to the Python
> community so everyone can have an interface to manipulating Id3 tags
> on MP3 files.

Get familiar with

    http://www.python.org/download/Contributed.html
and
    http://www.python.org/ftp/python/contrib/

> What's the accepted way of doing this? Is there an equivalent of Perl's
> CPAN for Python?

Afraid not -- not even close.  There are various collections and "guides"
scattered all over the place, and whenever I mention this some one or two
people reply outraged that I didn't name theirs specifically; then someone
else chimes in to repeat that someday Python will have a scheme *better*
than CPAN.  So, like I said, not even close <wink>.

although-someday-etc-ly y'rs  - tim






More information about the Python-list mailing list