[Tutor] Changing the Attribute of a Variable

wesley chun wescpy at gmail.com
Tue Feb 17 09:16:10 CET 2009


>> 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?
>
> You already received an answer to your immediate question, but I wanted to
> clarify: "strftime" is NOT an attribute, it's a method.  Calling the
> strftime method of a time object returns a string, formatted according to
> the pattern you specify - so what you're storing as "stop_time" is not a
> time, but a string.


this is a reply to both:

wayne: the initial setting of self.stop_time as a datetime.time object
seems to be ok, but i'm uncomfortable with the fact that after "[the]
user reads his [config] file," it becomes a str. i think that the code
that processes the config file should be setting self.stop_time as
another datetime.time object, so that way, when you want to set the
value for set_loc_dict, it would not have any problems calling its
strftime() method.

marc: i will slightly disagree with you with regards to strftime *not*
being an attribute. it *is* an attribute, just not a *data
attribute*... i call it a "function attribute," but that's just
terminology. any time you have an object x with an attribute y, the
fact that you can issue x.y means that y is indeed an attribute of x.
if it's a data attribute, you access it with x.y. if it's a function
attribute, i.e., a method, you also access it with x.y, esp. if you
want to pass the function object around, and finally, if you actually
want to *execute* it *and* it's a method, then you add the parens,
x.y().

hope this helps,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Python Web Development with Django", Addison Wesley, (c) 2009
http://withdjango.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