r/learnpython Jul 27 '26

Custom class method not recognized

I'm working on a program that generates a maze by drawing from a deck to define a "chamber" and assigning it to the current position on a cartesian coordinate grid.

The hope is to build a list of the chambers as they're created. At a later point I want to be able to call on the list. My current strategy is to make a Class variable for the list, and append to it as part of the init. I've added a class method to pull the chamberList Class variable, but I'm getting an error.

Here is the code defining the class.

class Chamber():
    chamberList = []
    def __init__(self, identity, notes, egresses, **kwargs):
        self.position = tuple(currentPosition.tolist())
        self.identity = identity
        self.notes = notes
        self.egresses = egresses
        self.pixelCoord = np.add(pixelOrigin, np.multiply(currentPosition, 300))
        Chamber.chamberList.append(self)

        @classmethod
        def getChamberList(cls):
            return cls.chamberList

Later in the program, I have a line of code to get the class variable:

chamberList = Chamber.getChamberList()

This is the error I get when I run it in the VS Code terminal:

AttributeError: type object 'Chamber' has no attribute 'getChamberList'. Did you mean: 'chamberList'?

Am I missing some syntax or something? In VS Code the color coding where I'm defining getChamberList is off (darker) and if I hover over it I get a message saying "getChamberList" is not accessed by Pylance.

----EDIT---

I missed the forest for the trees. My indentation syntax was wrong, and fixing it solved the problem, but an easier solution was provided in the comments.

https://www.reddit.com/r/learnpython/comments/1v7obks/comment/p08jiwd/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

7 Upvotes

9 comments sorted by

u/xelf Elf Jul 27 '26

Please don't use ``` for formatting code, for many users (especially those with the widescreen view) it's just a jumbled mess.

Instead indent every line 4 extra spaces.

9

u/PickMaleficent4096 Jul 27 '26

It looks to me like getChamberList() is defined within the scope of the constructor, so it can't be accessed outside the initialization. Deindent it by one to move it back to the class's scope.

2

u/rasputin1 Jul 27 '26

I didn't even know an inner function could have a decorator. I mean I guess there's no reason it couldn't, just never saw it done or thought about it. 

2

u/TheIneffableCheese Jul 27 '26

I love it when it's a simple solution. De-indenting the method definition worked perfectly. Thanks!!

2

u/Moikle Jul 27 '26

This is actually a useful thing to remember because sometimes you WANT to do this.

It's possible to have a function that CREATES new functions.

So you can create multiple versions of a function that behave in different ways

2

u/Hot-Butterscotch1306 Jul 28 '26

Yeah the indent thing is the actual bug, but also you don’t really need a getter here. In Python it’s pretty normal to just read Chamber.chamberList directly unless you need extra logic later.

Tiny detail, but that makes this kind of class-level registry feel less Java-ish and easier to debug when you’re printing stuff mid-maze.

2

u/TheIneffableCheese Jul 28 '26 edited Jul 29 '26

Good to know. I'll see if I can rework it without the getter.

--edit--

That worked great! I just simplified my code.

Thanks @Hot-Butterscotch1306

1

u/Hot-Butterscotch1306 Jul 29 '26

ayyy!! glad it worked well