Python Forum

Full Version: Class - Error Message (Indentation)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a writing a Class definition. However, I got error msg: 

Error:
SyntaxError: inconsistent use of tabs and spaces in indentation
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 = self.get_Prev()
I'm not sure why I got the error message. The indentation seems consistent to me. The Class definition would work until I introducted the last 2 lines of code: 

def get_Arg(self):

                                self.date = self.get_Prev()
Wonder if someone could point out to me how I could resolve this.

Thank you!

After I retyped the whole thing, it works now.
Smile
4 spaces shall indents be; no more; no less.

Thou shall not indent 3 spaces unless immediately followed by a fourth.
5 is right out.

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 = self.get_Prev()
Any editor worth using allows you to set the tab key to insert spaces rather than tabs.  If your editor doesn't allow this you need to use a different one.
Python will accept any indentation level even 32 spaces but 4 is a standart, the code is readable and doesn't waist the screen space