0

I have a class that is inheriting from another class, and I get the issue:

Traceback (most recent call last):
  File "main.py", line 45, in <module>
    class Player(Entity):
  File "main.py", line 53, in Player
    self.image = pygame.image.load('sam_stand.png')
NameError: name 'self' is not defined

These are the classes:

class RigidBody(object):
    def __init__(self, (x, y), size, mass=1):
        self.x = x
        self.y = y
        self.size = size
        self.mass = mass
        self.thickness = 0
        self.angle = 0
        self.drag = 1
        self.elasticity = 0.9

class Player(Entity):
    """Player class.  Provides all player variables and methods"""
    def __init__(self):
        RigidBody.__init__(self)
        self.grounded = True
        self.direction = "Right"
        self.axis = "Down"
        self.jump_counter = 0
        self.image = pygame.image.load('sam_stand.png')

How come self is recognized for all the other attributes for the Player, except for self.image? If I change it to image = pygame.image.load('sam_stand.png') the problem goes away.

19
  • 3
    Your traceback is incomplete, you forgot to include the exception message. Commented Oct 9, 2015 at 21:56
  • 2
    with RigidBody.__init__(self) you are calling RigidBody's constructor passing it your present object (i.e. an instance of Player).... what is your goal? Commented Oct 9, 2015 at 21:57
  • 3
    @MartijnPieters Entity != RigidBody, is it not? Commented Oct 9, 2015 at 21:59
  • 3
    @Pynchia: good point. and the RigidBody.__init__() method takes positional (required!) arguments! Commented Oct 9, 2015 at 22:00
  • 3
    @PeterWood: you can in Python 2. Commented Oct 9, 2015 at 22:02

1 Answer 1

4

You are mixing tabs and spaces. When looking at your first revision source I see this:

enter image description here

Your method body is indented with tabs, which Python expands to 8 spaces. The last line, however, is indented with spaces only. You have your editor set to 4 spaces per tab, so you cannot see this mistake.

As a result, the self.image line falls outside the __init__ method. It is part of the class definition instead.

You really want to configure your editor to indent with spaces only.

Run your code with python -tt scriptname.py and fix all the errors that reports. Then run the tabs-to-spaces feature in your text editor (converting to 4 spaces) and then configure it to use spaces for indentation (automatically inserting 4 spaces when you use the tab key).

Using spaces for indentation is recommended by the Python styleguide for a reason, after all.

Sign up to request clarification or add additional context in comments.

4 Comments

I see. Wow, weird. I guess that would make sense. I will change all that and save myself a ton of trouble in the future. Thanks guys
I think you are also going to have to revisit the line RigidBody.__init__(self). LOL
@RobertB: that's a separate issue, not the cause of the error. The revisiting is happening in the comments on the question.
I agree. You solved it and I gave you the up-vote. Just letting him know that that will be his next error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.