How to reference the class itself in python

python

Solution

You need `@classmethod` rather than `@staticmethod` - a class method will get passed a reference to the class (where a method will get `self`), so you can look up attributes on it.

class FooBarBaz:
    BAR = 123

    @classmethod
    def getBar(cls):
        return cls.BAR

Problem

I want to have simple class constants in python like this: ``` class FooBarBaz: BAR = 123 @staticmethod def getBar(): return BAR # this would not work, of course return FooBarBaz.BAR # this would work but is "way too long" ``` Is there a shorter way to reference the class itself from inside a method, not the current instance? It's not only for static methods but in general, like a `__class__` keyword or something.

Original source

Related problems