Marijke Vonk
Marijke Vonk

Reputation: 418

Class does not recognize attribute?

This is driving me crazy. I want the program to print the name "Frank". However, I get an error that says "AttributeError: 'People' object has no attribute 'called'". I've searched online and as far as I can tell I'm doing everything right, but since I'm getting an error obviously I'm not.

I'm using Python 2.

class People(object):

    def __init__(self, name):
        self.name = name

    def called(self):
        return self.name

frank = People("Frank")

print frank.called()

What am I doing wrong?Added screenshot for proof

Upvotes: 3

Views: 4857

Answers (1)

Kevin
Kevin

Reputation: 76184

Looks like an indentation problem.

enter image description here

Dots are spaces, arrows are tabs.

Even though tabs may look like they're equivalent to four spaces in your text editor, Python may not interpret them as such. As a result, you should never mix tabs and spaces. Use only one or the other. Preferably spaces.


I see you're using Notepad++. For ease of debugging, you can configure it to display tabs as arrows by going to View -> Show Symbol, and checking "Show White Space and TAB". You can also configure it to insert four spaces instead of a tab character when you press the tab key, via Settings -> Preferences -> Language Menu/Tab Settings -> Replace by space.

Upvotes: 9

Related Questions