Created
June 28, 2012 16:44
-
-
Save stesh/3012428 to your computer and use it in GitHub Desktop.
hacky decorator for class-level properties in python using @classmethods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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