Existance of of variable

Josiah Manson slomojo83 at gmail.com
Mon Jul 4 20:42:30 CEST 2005

Hello. I am very new to Python, and have been unable to figure out how
to check if a variable exists or not. In the following code I have made
a kludge that works, but I think that it would be clearer to check if
closest exists and not have to initialize it in the first place. How is
that check done?

The following code finds the closest place to a position and rejects
places that are too far away.

        dist = 1e9
        closest = -1

        for n,p in galaxy.places.iteritems():
            dif = p.pos - pos
            len = dif.len()
            if len < dist and len < 10.0/self.zoom:
                dist = len
                closest = p

        if closest != -1:
            self.sel = [closest.name]

I also have a few other questions to tack on if you don't mind. I am
setting dist to 1e9, because that is a larger value than any of the
places in the galaxy will be far away. Is there a better way to
initialize dist so that it is impossible for this to fail? For example,
would setting dist to infinity work, and how is that done?

Extending my existance checking question, how does one check what type
a variable has?

Thanks for your help!

-- Josiah

More information about the Python-list mailing list