Python Forum
AttributeError: 'NoneType' object has no attribute 'n' in list of class objects
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'NoneType' object has no attribute 'n' in list of class objects
#1
So, I was trying to develop a game in PyGame and came across a problem. I tried it it on a much simpler code and it still returns an error, so I think it's not about my big code. Here's what goes wrong (the short version)
from random import randint as rand

def A():
    def __init__(self):
        self.n = rand(1,10)

b = []
for i in range(10):
    b.append(A())
for i in range(10):
    print(str(b[i].n))
So, basically, I'm filling "b" with a list of 10 "a" objects. Each object gets assigned a random value on its __init__. Then I iterate through all the objects printing their "n" value. Well, instead of printing some random numbers, it gives me an error:
Error:
Traceback (most recent call last): File "C:/Users/sergio/Desktop/python/test/d.py", line 11, in <module> print(str(b[i].n)) AttributeError: 'NoneType' object has no attribute 'n'
Any help would be appreciated.
Reply
#2
If you want an object you have to create a class, not a function:

class A():
    def __init__(self):
        self.n = rand(1,10)
Reply
#3
Oh that's true. And now that code works but my long code still doesn't. Here it is https://pastebin.com/Z4qjGxCU. The problem with this one is that, when I call check_num() on the tiles, it works. But when I call check_mouse(), load_image() [which I've commented so you don't have to download the assets] or print_tile() they return an error such as this:
Error:
Traceback (most recent call last): File "C:\Users\sergio\Desktop\Minesweeper\minesweeper.py", line 96, in <module> grid[i][j].check_mouse() AttributeError: 'Tile' object has no attribute 'check_mouse'
I've checked the code but I can't find anything that's wrong. Any ideas?
Reply
#4
You have an "extra" tab at the 56 line.
Align the "def check_mouse(self):" like "def check_num(self):"
And the other functions too.
Reply
#5
Oh god, that was so simple. I always overlook the most noticeable errors somehow. Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: 'NoneType' re.search philnyland 1 252 Jan-20-2024, 03:24 AM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 456 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 679 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  How to read module/class from list of strings? popular_dog 1 425 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 721 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  TypeError: 'NoneType' object is not callable akbarza 4 921 Aug-24-2023, 05:14 PM
Last Post: snippsat
  How can I access objects or widgets from one class in another class? Konstantin23 3 942 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,533 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,385 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 704 Jun-22-2023, 02:33 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020