Skip to content

Instantly share code, notes, and snippets.

@stesh
Created June 28, 2012 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stesh/3012428 to your computer and use it in GitHub Desktop.
Save stesh/3012428 to your computer and use it in GitHub Desktop.
hacky decorator for class-level properties in python using @classmethods
class classproperty(property):
def __get__(self, cls, inst):
return self.fget.__get__(None, inst)()
class MyClass(object):
numbers = [1,2,3,4,5]
@classproperty
@classmethod
def has_numbers(cls):
return len(cls.numbers) > 0
# no instance of MyClass has been created
if MyClass.has_numbers:
print 'yay!, we have numbers!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment