If anyone still has this issue: you get this error when your indentation is goofed.To fix the asked question above, you just have to add a space before the last two functions definitions, that is;

class className(object):
    

    def __init__(self, self1=1,self2=2,self3=3):
        self.self1=self1
        self.self2=self2
        self.self3=self3

    def evaluate(self, self5):
        
        print className.func1(self) + className.func2(self)
        self.self5=self5
        print className.func1(self)

    def func1(self):
        return self.self1 + self.self5

    def func2(self):
        self.self4 = self.self1+self.self2+self.self3
        return self.self4

just make sure they all have similar indentation, and you are good to go.

Answer from michael ditrick on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › object has no attribute error, but it actually does..
r/learnpython on Reddit: Object has no attribute error, but it actually does..
September 17, 2022 -

hello, i am working on a game using pygame and i am going for an OOP approach, the files are pretty big, i will post only a portion of the code; also i am using pycharm so i know for a fact my identation, typing are alright.. plus i have been coding in python for a while now but i just can't grasp onto this issue :(, so far in the game i am just in the main menu phase and whenever i try to run my code i get an error that my game object (which has a reference to another class including the main menu game loop) has no attribute ''screenWidth'' but it actually does have a variable for my screen.. i just use it in the main menu to set another midWidth variable half of that variable as value
here is the code:

main.py

from game import Game

myGame = Game()

while myGame.running:
    myGame.currentMenu.drawMainMenu()
    myGame.gameLoop()

game.py

import pygame
from menu import MainMenu


class Game:
    def __init__(self):
        pygame.init()

        self.running, self.playing = True, False
        self.upKey, self.downKey, self.selectKey, self.backKey = False, False, False, False

        self.currentMenu = MainMenu(self)

        self.screenWidth = 1280
        self.screenHeight = 720
        self.gameWindow = pygame.display.set_mode((self.screenWidth, self.screenHeight))
        pygame.display.set_caption("Game Prototype")

        self.backgroundImage = pygame.image.load('Assets/Images/bg_greek.jpg')
        self.backgroundImage = pygame.transform.scale(self.backgroundImage, (1920, 1080))

    def gameLoop(self):
        while self.playing:
            self.checkEvents()

            if self.selectKey:
                self.playing = False

            self.gameWindow.blit(self.backgroundImage, (-600, -300))

            pygame.display.flip()
            self.resetKeys()

menu.py

import pygame


class Menu:
    def __init__(self, game):
        self.gameClass = game
        self.midWidth = self.gameClass.screenWidth / 2
        self.midHeight = self.gameClass.screenHeight / 2
        self.showMenu = True
        self.selectionX, self.selectionY = 0, 0

    def drawSelection(self):
        selectSurface = pygame.Surface((260, 100), pygame.SRCALPHA)
        selectSurface.fill((50, 50, 50, 175))
        selectRect = selectSurface.get_rect()
        selectRect.center = (self.selectionX, self.selectionY)
        self.gameClass.gameWindow.blit(selectSurface, selectRect)


class MainMenu(Menu):
    def __init__(self, game):
        Menu.__init__(self, game)
        self.hoveredState = "Start"
        self.startX, self.startY = self.midWidth, self.midHeight
        self.optionsX, self.optionsY = self.midWidth, self.midHeight + 100
        self.creditsX, self.creditsY = self.midWidth, self.midHeight + 200
        self.quitX, self.quitY = self.midWidth, self.midHeight + 300
        self.selectionX, self.selectionY = self.startX, self.startY

    def drawMainMenu(self):
        self.showMenu = True
        while self.showMenu:
            self.gameClass.checkEvents()
            self.checkInput()

            self.gameClass.gameWindow.blit(self.gameClass.backgroundImage, (-600, -300))
            self.drawSelection()
            self.gameClass.drawText("Play", 72, self.startX, self.startY)
            self.gameClass.drawText("Options", 72, self.optionsX, self.optionsY)
            self.gameClass.drawText("Credits", 72, self.creditsX, self.creditsY)
            self.gameClass.drawText("Quit", 72, self.quitX, self.quitY)

            pygame.display.flip()
            self.gameClass.resetKeys()

it is not complete though

Discussions

pyqgis - AttributeError: class instance has no attribute 'class_function' - Geographic Information Systems Stack Exchange
When working in my plugin's main python function, I receive an AttributeError anytime I call a class method from within the class itself. For example the sample code below: class PluginName: ... More on gis.stackexchange.com
🌐 gis.stackexchange.com
March 5, 2018
Python "Class has no attribute" - Stack Overflow
I'm new to Python and I'm learning about classes and functions and I want to print a class's function but all i get is the error "Class has no attribute" items.py: class Item(): def __init___... More on stackoverflow.com
🌐 stackoverflow.com
Python AttributeError: class object has no attribute - Stack Overflow
Your code style is somewhat... ... initialising self.markers. Given that self.markers appears to be fixed, why not make it a class attribute? ... Please do post a full traceback for Python errors.... More on stackoverflow.com
🌐 stackoverflow.com
python - Getting an AttributeError: <class> has no attribute <method> - Stack Overflow
Chat room owners can now establish room guidelines · Opinion-based questions alpha experiment on Stack Overflow · 2 How to identify if Python Threads with Queue are done with task? 2 sklearn transformation pipeline and featureunion · 0 Getting error: AttributeError: class has no ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How to solve the error in Python, "Class object has no attribute_name"?
To solve this error, we should check if the attribute is declared in the class or not. Further, if the attribute name has typos and case differences or if is declared privately in the class, it will lead to error at runtime.
🌐
askpython.com
askpython.com › home › how to fix the ‘class’ object has no ‘attribute_name’ error in python
How to Fix the 'Class' object has no 'attribute_name' Error in ...
Which Python method can prevent Attribute Errors?
Most of the time, an Attribute Error occurs because either the attribute is missing, is out of scope (private) or the name contains typos and case differences. Thus to check for such cases Python provides us a method named "hasattr()'. The method is a boolean function and returns true and false, after the check.
🌐
askpython.com
askpython.com › home › how to fix the ‘class’ object has no ‘attribute_name’ error in python
How to Fix the 'Class' object has no 'attribute_name' Error in ...
🌐
AskPython
askpython.com › home › how to fix the ‘class’ object has no ‘attribute_name’ error in python
How to Fix the 'Class' object has no 'attribute_name' Error in Python - AskPython
April 10, 2025 - The “AttributeError: ‘Class’ object has no ‘attribute'” error in Python occurs when an object tries to access an attribute not defined on its class, often due to typos, missing attributes, scope issues with private attributes, or ...
🌐
Delft Stack
delftstack.com › home › howto › python › python object has no attribute
How to Fix Object Has No Attribute Error in Python | Delft Stack
February 2, 2024 - The error shows because the function called is not associated with the B class. We can tackle this error in different ways. The dir() function can be used to view all the associated attributes of an object. However, this method may miss attributes inherited via a metaclass. We can also update our object to the type that supports the required attribute. However, this is not a good method and may lead to other unwanted errors. We can also use the hasattr() function.
Top answer
1 of 3
4

This is really late, but in case anyone has this problem again, as I did, and looks here, hopefully this helps them.

Some text editors such as Sublime Text occasionally mess up the tabs, so the spacing you see in the text editor is not necessarily what Python sees. Since tabs are important in Python, this can lead to your do_something function being defined within the init function rather than as a separate function. Hence, when you call self.do_something(), Python will not have created the function yet and it will fail.

To fix this, open the file in another text editor. I find the simplest text editors like 'Text Editor' in Ubuntu, 'Notepad' in Windows, or 'TextEdit' in Mac work best. You will likely see immediately where the spacing went wrong and can fix it there. If it is not immediately clear, try deleting the tabs and redoing them.

This fixed the problem for me. Thanks.

2 of 3
2

I was getting the same error for a long time, I am new to this, but in my function when I remove the underscores from the function name it works fine. I figured underscores are treated in some different way in django.

def getname(self):
    return self.name

def getname1(self):
    return self.name

def lastseen(self):
    return cache.get('seen_%s' % (self.user.username))



def last_seen(self):
    return cache.get('seen_%s' % (self.user.username))

The .last_seen() doesn't work, The .lastseen() does, The .getname1() doesn't work, The .getname() does.

To be honest I don't know if anyone does I would love to know why. Thanks

🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-fix-attributeerror-object-has-no-attribute
How to fix AttributeError: object has no attribute - GeeksforGeeks
July 23, 2025 - The def __init__(self, name) is the class's constructor method. It takes two parameters, self (the instance being created) and name (the dog's name). Inside the constructor, we are initializing the name attribute of the Dog object with the value provided as name. Then we are creating a Dog object named my_dog with the name "Buddy". But in the end we are trying to access the non-existent attribute "breed".
🌐
sebhastian
sebhastian.com › object-has-no-attribute-python-class
How to fix AttributeError: object has no attribute in Python class | sebhastian
February 17, 2023 - Traceback (most recent call last): File "main.py", line 10, in <module> person.eat() AttributeError: 'Human' object has no attribute 'eat' To fix this you need to define the eat() method inside the class as follows: class Human: def __init__(self, ...
Find elsewhere
Top answer
1 of 3
2

Gold is not an attribute on the Item class, no. It is a subclass, and a global name in its own right. You can import it from your items module:

>>> from items import Gold
>>> Gold
<class 'items.Gold'>

You cannot create an instance of it, because used the wrong name for the Item.__init__ method:

>>> from items import Item
>>> Item.__init__
<slot wrapper '__init__' of 'object' objects>
>>> Item.__init___
<function Item.__init___ at 0x1067be510>
>>> Item('a', 'b', 4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object() takes no parameters

Note that the method you created has three underscores in the name. If you fix that:

class Item():
    def __init__(self, name, desc, val):
        # ^   ^ 2 underscores on both sides
        self.name   = name
        self.desc   = desc
        self.val    = val

you can create instances of the Gold() class:

>>> Gold()
<items.Gold object at 0x1067cfb00>
>>> gold = Gold()
>>> print(gold.print_info())
Gold
==========
Golden coin.

Value: 5

Now, if you really wanted to create attributes on the Item class, you'll have to add those after you created the class:

class Item():
    def __init___(self, name, desc, val):
        self.name   = name
        self.desc   = desc
        self.val    = val

    def print_info(self):
        return '{}\n==========\n{}\n\nValue: {}'.format(self.name, self.desc, self.val)

Item.gold = Item('Gold', 'Golden coin.', 5)

You don't need to create subclasses for that. You could use the enum module here though:

from enum import Enum

class Item(Enum):
    Gold = 'Golden coin.', 5
    Silver = 'Silver coin.', 1

    def __init__(self, desc, val):
        self.desc = desc
        self.val = val

    def print_info(self):
        return '{}\n==========\n{}\n\nValue: {}'.format(self.name, self.desc, self.val)

Here Gold is an attribute of Item:

>>> Item
<enum 'Item'>
>>> Item.Gold
<Item.Gold: ('Golden coin.', 5)>
>>> print(Item.Gold.print_info())
Gold
==========
Golden coin.

Value: 5
>>> Item.Silver
<Item.Silver: ('Silver coin.', 1)>
2 of 3
0

Here's what you're doing wrong:

  • Gold is a subclass of Item, not an attribute of it. Your error is popping up when you try to do Item.Gold. Gold is accessed entirely separately.
  • You need to instantiate your classes into objects. Once you instantiate an object, you can call your methods on it and access its attributes. Each object stores methods and attributes independently, so one gold coin can have a different name, description, value, or even print its info differently.
  • When trying to access a parent class from within a subclass, you just reference the class name directly rather than using super().
  • You have an extra underscore in your Item class's __init__()

So with that in mind, your new main.py should look like this:

from items import Gold

mygold = Gold() # This is where we instantiate Gold into an object
print(mygold.print_info()) # We call the method on the object itself

And your items.py will look like this:

class Item():
    def __init__(self, name, desc, val):
        self.name   = name
        self.desc   = desc
        self.val    = val

    def print_info(self):
        return '{}\n==========\n{}\n\nValue: {}'.format(self.name, self.desc, self.val)

class Gold(Item):
    def __init__(self):
        Item.__init__(name = "Gold", desc = "Golden coin.", val = str(5))
🌐
JanBask Training
janbasktraining.com › community › python-python › why-am-i-getting-attributeerror-object-has-no-attribute
Why am I getting AttributeError: Object has no attribute | JanBask Training Community
April 17, 2021 - The "AttributeError: Object has no attribute" error occurs in Python when you try to access or call an attribute or method that does not exist on an object. Here’s how you can troubleshoot and resolve this issue: ... Ensure that the attribute ...
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 171777 › error-object-has-no-attribute-xxx
python - Error: object has no attribute 'xxx' [SOLVED] | DaniWeb
In that case it is a local function, not bound to the class, so Grid (and its instances) will not have that attribute. A quick sanity check while debugging is to verify membership on the class, not the instance: print(hasattr(Grid, 'printGrid2')). ...
🌐
Python Forum
python-forum.io › thread-5353.html
AttributeError: type object 'MyClass' has no attribute 'channel'
Hi all I have a trouble with getting the list of self.channel when I am trying to get the list of strings from my another script called test.py. In player.py when I have input import test, I am getting an error: AttributeError: type object 'MyClass'...
🌐
CodeWithHarry
codewithharry.com › blogpost › attribute-error-in-python
[Solved] Python AttributeError: object has no attribute 'X' | Blog | CodeWithHarry
April 5, 2025 - The error emerges because the __init__ method of the Car class does not initialize the speed attribute at all. class BankAccount: interest_rate = 0.05 my_account = BankAccount() print(my_account.interest_rate) # Attribute Error: 'BankAccount' object has no attribute 'interest_rate'
🌐
GitHub
github.com › Koed00 › django-q › issues › 613
'class' object has no attribute '__name__' · Issue #613 · Koed00/django-q
September 14, 2021 - I’m calling the async_task on a class function. I’m getting the error 'FlightSearch' object has no attribute 'name' views.py from .search import FlightSearch def index(request): html = " hi " FlightSearch(1).searc...
Author   sepehrsafa
🌐
GeeksforGeeks
geeksforgeeks.org › python-attributeerror
Python: AttributeError - GeeksforGeeks
January 3, 2023 - Traceback (most recent call last): File "/home/2078367df38257e2ec3aead22841c153.py", line 3, in string = "The famous website is { }".fst("geeksforgeeks") AttributeError: 'str' object has no attribute 'fst' Example 3: AttributeError can also be raised for a user-defined class when the user tries to make an invalid attribute reference. ... # Python program to demonstrate # AttributeError class Geeks(): def __init__(self): self.a = 'GeeksforGeeks' # Driver's code obj = Geeks() print(obj.a) # Raises an AttributeError as there # is no attribute b print(obj.b)