[Tutor] Changing the Attribute of a Variable

wesley chun wescpy at gmail.com
Tue Feb 17 06:26:47 CET 2009


On Mon, Feb 16, 2009 at 8:24 PM, Wayne Watson
<sierra_mtnview at sbcglobal.net> wrote:
> I suspect I'm in need of setattr for this in a GUI program I'm modifying.
>
> Initally, a variable. self.stop_time is created as type datetime.time, with
> the default value 06:00:00, a time stamp, during entry into the mainloop.
> self.stop_time = datetime.time(10,10,10). The user reads his configuration
> file with the time stamp value of 08:00:00. self.time_stop becomes type
> string. He then goes to a dialog to change time stop, and it tries to access
> self.time_stop, and find is needs to have the attribute strftime, as in:
>         set_loc_dict[ "stop_time" ] = self.stop_time.strftime("%H:%M:%S")
>
> When reading the configuration file, how do I make sure that self.time_stop
> is really has the strftime attribute?


close. what you need is hasattr(), which returns True/False. you can
also use getattr() with a default value as well. the problem you're
facing is that the object is now a str and not a datetime.time as you
mentioned. you have 2 options:

a. keep everything as a string and manipulate the value that way, or
b. convert the string to a datetime.time object and keep it like that

good luck!
-wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list