Jan-23-2017, 04:03 AM
Hi,
I'm writing a class Sunday
1) to get datetime of previous Sunday (method)
2) to return the following attributes:
a) day (in string)
b) month (in string)
c) year (in string)
I then did the following:
However, I got the following error message.
I then tested out the following:
Wonder if someone could point out to me, how I could call get_Prev method within the class? I'm trying to define a second method get_Arg() so that I could add a few more lines of codes to convert the attributes day, month and year into string. E.g. sun.day_str, sun.month_str, sun.year_str
I'm writing a class Sunday
1) to get datetime of previous Sunday (method)
2) to return the following attributes:
a) day (in string)
b) month (in string)
c) year (in string)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Sunday: def __init__( self , dayname, start_date = datetime.today()): self .dayname = dayname self .date = start_date self .day = start_date.day self .month = start_date.month self .year = start_date.year self .weekday = start_date.weekday() self .weekdays = [ 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' , 'Sunday' ] def get_Prev( self ): day_num = self .weekday day_num_target = self .weekdays.index( self .dayname) days_ago = ( 7 + day_num - day_num_target) % 7 if days_ago = = 0 : days_ago = 7 self .date = self .date - timedelta(days = days_ago) self .day = self .date.day self .month = self .date.month self .year = self .date.year self .weekday = self .date.weekday() def get_Arg( self ): self .date = get_Prev() |
1 2 3 |
sun = Sunday( "Sunday" ) sun.get_Arg() |
Error:NameError: name 'get_Prev' is not defined
I tried the following to see if I get a different result.1 2 3 |
def get_Arg( self ): self .date = self .get_Prev() |
1 2 3 |
sun = Sunday( "Sunday" ) sun.get_Arg() type (sun.date) |
Output:<class 'NoneType'>
No error message but I was thinking that it will call get_Prev() method within the class but it doesn't like it did.Wonder if someone could point out to me, how I could call get_Prev method within the class? I'm trying to define a second method get_Arg() so that I could add a few more lines of codes to convert the attributes day, month and year into string. E.g. sun.day_str, sun.month_str, sun.year_str