Python Forum
calling Class method inside the Class definition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling Class method inside the Class definition
#1
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)

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()
I then did the following:

sun = Sunday("Sunday")

sun.get_Arg()
However, I got the following error message.

Error:
NameError: name 'get_Prev' is not defined
I tried the following to see if I get a different result.

def get_Arg(self):

 self.date = self.get_Prev()
I then tested out the following:

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
Reply
#2
This is correct (syntactically):
def get_Arg(self):
 self.date = self.get_Prev()
The reason you are ending up with none is that the method get_prev doesn't actually return anything so you are setting date to none.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class and runtime akbarza 4 277 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 421 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 561 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 402 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,713 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 525 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE
  error in class: TypeError: 'str' object is not callable akbarza 2 444 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  super() in class akbarza 1 401 Dec-19-2023, 12:55 PM
Last Post: menator01
  mutable argument in function definition akbarza 1 423 Dec-15-2023, 02:00 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020