tags:

views:

121

answers:

3

my code:

import time
print hasattr(time.tzset)#error

and why someone do this like next:

if hasattr(time, 'tzset'):
            # Move the time zone info into os.environ. See ticket #2315 for why
            # we don't do this unconditionally (breaks Windows).
            os.environ['TZ'] = self.TIME_ZONE
            time.tzset()

i can't understand.

thanks

+2  A: 

Either you have a local module that is shadowing the stock time module, you're using a version of Python older than 2.3, or you're running Python on Windows.

Ignacio Vazquez-Abrams
+2  A: 

Your use of hasattr is wrong. The correct syntax is shown in your second snippet.

hasattr takes two arguments - an object, and a string represent the attribute you want to check for. The way you've done it, Python will try to evaluate time.tzset first, before passing it to hasattr - thus causing the very error you're trying to avoid.

Daniel Roseman
A: 

See the docs for tzset: they clearly say

Availability: Unix.

so you would have it in, say, MacOSX, Solaris, or Linux, but not on Windows.

Also: there is no such thing as "your time class", despite your Q's title: the time you're trying to use is a module, not a class.

And finally, as @Daniel says, your first use of hasattr is totally wrong (the second one, which you don't understand, is correct).

Alex Martelli