Search code examples
pythonstatic-variables

NameError in Python when listing static attributes from within same class


I have the following python class:

class list_stuff:    
        A = 'a'
        B = 'b'
        C = 'c'
        stufflist = [v for k,v in list_stuff.__dict__.items() if not k.startswith("__")]

But it shows a NameError saying undefined variable list_stuff.

According to this, it should work.

I also tried with:

list_stuff().__dict__.items()

But still same error. What am I missing here?


Solution

  • I ended up doing this:

    class list_stuff:    
        A = 'a'
        B = 'b'
        C = 'c'
    
        @classmethod
        def stufflist(cls):
            return [v for k,v in cls.list_stuff.__dict__.items() if not k.startswith("__")]
    

    which has the same effect as my original intent.

    Thanks all for quick replies.