Paul Bentham
Paul Bentham

Reputation: 366

NameError: name 'self' is not defined, even though it is?

Can anyone helps me understand why this is this giving me an error? The error being "NameError: name 'self' is not defined". I have a similar class higher up in my code and that works fine?

I'm using 'xlrd' and team is a reference to a workbook.sheet_by_name.

class Rollout:                                  
    def __init__(self, team, name):
        self.team = team
        self.name = name
        self.jobs = {}
        self.start_row = 1
        self.last_row = self.team.nrows

    for i in range(self.start_row,self.last_row):
            try:
                self.jobs[i-1] =   [str(self.team.cell_value(i,0)).upper(), \
                                    str(self.team.cell_value(i,1)).upper(), \
                                    str(self.team.cell_value(i,2)).upper(), \
                                    str(self.team.cell_value(i,3)).upper(), \
                                    str(xlrd.xldate_as_tuple(self.team.cell_value(i,4),0)[3]), \
                                    str(self.team.cell_value(i,5)).upper(), \
                                    str(self.team.cell_value(i,6)).upper()]
            except ValueError:
                print "It look's like one of your 'time' cells is incorrect!"
                self.jobs[i-1] =   [str(self.team.cell_value(i,0)).upper(), \
                                    str(self.team.cell_value(i,1)).upper(), \
                                    str(self.team.cell_value(i,2)).upper(), \
                                    str(self.team.cell_value(i,3)).upper(), \
                                    "missing", \
                                    str(self.team.cell_value(i,5)).upper(), \
                                    str(self.team.cell_value(i,6)).upper()]

Upvotes: 12

Views: 77371

Answers (5)

questionto42
questionto42

Reputation: 9532

The accepted answer already tells this, here is just the fixed code of the question, with the for loop indented in the __init__ function:

class Rollout:                                  
    def __init__(self, team, name):
        self.team = team
        self.name = name
        self.jobs = {}
        self.start_row = 1
        self.last_row = self.team.nrows

        for i in range(self.start_row,self.last_row):
                try:
                    self.jobs[i-1] =   [str(self.team.cell_value(i,0)).upper(), \
                                        str(self.team.cell_value(i,1)).upper(), \
                                        str(self.team.cell_value(i,2)).upper(), \
                                        str(self.team.cell_value(i,3)).upper(), \
                                        str(xlrd.xldate_as_tuple(self.team.cell_value(i,4),0)[3]), \
                                        str(self.team.cell_value(i,5)).upper(), \
                                        str(self.team.cell_value(i,6)).upper()]
                except ValueError:
                    print "It look's like one of your 'time' cells is incorrect!"
                    self.jobs[i-1] =   [str(self.team.cell_value(i,0)).upper(), \
                                        str(self.team.cell_value(i,1)).upper(), \
                                        str(self.team.cell_value(i,2)).upper(), \
                                        str(self.team.cell_value(i,3)).upper(), \
                                        "missing", \
                                        str(self.team.cell_value(i,5)).upper(), \
                                        str(self.team.cell_value(i,6)).upper()]

Upvotes: 0

user13756678
user13756678

Reputation: 1

An example of a class

class Shape:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    description = "This shape has not been described yet"
    author = "Nobody has claimed to make this shape yet"
    def area(self):
        return self.x * self.y
    def perimeter(self):
        return 2 * self.x + 2 * self.y
    def describe(self,text):
        self.description = text
    def authorName(self,text):
        self.author = text
    def scaleSize(self,scale):
        self.x = self.x * scale
        self.y = self.y * scale

Upvotes: -3

Yekatandilburg
Yekatandilburg

Reputation: 187

If you omit the "self" keyword in your Constructor's ( init) argument, then you will get:

NameError: name 'self' is not defined

whenever you use "self" in the constructor method body.

Upvotes: 0

Arulraj
Arulraj

Reputation: 423

I got the same error while executing my code,

#An example of a class
class Shape:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    description = "This shape has not been described yet"
    author = "Nobody has claimed to make this shape yet"
    def area(self):
        return self.x * self.y
    def perimeter(self):
        return 2 * self.x + 2 * self.y
    def describe(self,text):
        self.description = text
    def authorName(self,text):
        self.author = text
    def scaleSize(self,scale):
        self.x = self.x * scale
    self.y = self.y * scale

Then i kept space before last line of the codes like

#An example of a class
class Shape:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    description = "This shape has not been described yet"
    author = "Nobody has claimed to make this shape yet"
    def area(self):
        return self.x * self.y
    def perimeter(self):
        return 2 * self.x + 2 * self.y
    def describe(self,text):
        self.description = text
    def authorName(self,text):
        self.author = text
    def scaleSize(self,scale):
        self.x = self.x * scale
        self.y = self.y * scale

Then got no error, So space is the problem..

Upvotes: -1

Kyle Kelley
Kyle Kelley

Reputation: 14144

The for loop is indented incorrectly resulting in it being outside that method's scope but inside the class' scope. This in turn means that self is not defined.

Python does interpret that loop code in the scope of the class, but without an instance of the object. Sample malformed code:

class Simple(object):
    def __init__(self, a):
        self.a = a

    print("Here we go!")
    for i in xrange(self.a):
        print(i)

Traceback

$ python simple.py
Here we go!
Traceback (most recent call last):
  File "simple.py", line 4, in <module>
    class Simple(object):
  File "simple.py", line 9, in Simple
    for i in xrange(self.a):
NameError: name 'self' is not defined

Upvotes: 19

Related Questions