[Tutor] Datetime object as a class property
David Rock
david at graniteweb.com
Mon Aug 31 13:42:50 EDT 2020
> On Aug 31, 2020, at 10:39, John Weller <john at johnweller.co.uk> wrote:
>
> I have an instance of a class called, say, data. I know I can create a
> property, name, in the class as a string with self.name = '' which I can
> then populate in an instance of my class with data.name = 'John'. How do I
> create datetime object, eg sunrise, so that in the code I can put
> data.sunrise = datetime.strptime("05:45:00", "%H:%M:%S"). Self.sunrise = ?
If I understand correctly, you want data.sunrise to contain a datetime object. There is no need to specify a specific data type in the class; python variable names just point to whatever data type is associated to them. If you assign a datetime object to data.sunrise, it will be a datetime object. The simplest way to have a “placeholder” would be to assign None to self.sunrise:
self.sunrise = None
then once you create the data instance, data.sunrise can be associated to datetime.strptime("05:45:00", "%H:%M:%S”)
—
David Rock
david at graniteweb.com
More information about the Tutor
mailing list