Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why my 'time' class has not 'tzset' attribute

Tags:

python

syntax

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

like image 227
zjm1126 Avatar asked Jan 01 '10 08:01

zjm1126


3 Answers

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).

like image 120
Alex Martelli Avatar answered Oct 27 '22 00:10

Alex Martelli


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.

like image 25
Daniel Roseman Avatar answered Oct 27 '22 00:10

Daniel Roseman


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.

like image 39
Ignacio Vazquez-Abrams Avatar answered Oct 26 '22 23:10

Ignacio Vazquez-Abrams