[Tutor] Datetime object as a class property

John Weller john at johnweller.co.uk
Mon Aug 31 13:50:34 EDT 2020


Excellent!!  Many thanks.

John

John Weller
01380 723235
07976 393631

> -----Original Message-----
> From: Tutor <tutor-bounces+john=johnweller.co.uk at python.org> On Behalf Of
> David Rock
> Sent: 31 August 2020 18:43
> To: tutor <tutor at python.org>
> Subject: Re: [Tutor] Datetime object as a class property
> 
> 
> > 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
> 
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list