| Line | |
|---|
| 1 | # Copyright: 2006 Marien Zwart <marienz@gentoo.org> |
|---|
| 2 | # License: GPL2 |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | """Classes implementing the descriptor protocol.""" |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | class classproperty(object): |
|---|
| 9 | |
|---|
| 10 | """Like the builtin C{property} but takes a single classmethod. |
|---|
| 11 | |
|---|
| 12 | Used like this: |
|---|
| 13 | |
|---|
| 14 | class Example(object): |
|---|
| 15 | |
|---|
| 16 | @classproperty |
|---|
| 17 | def test(cls): |
|---|
| 18 | # Do stuff with cls here (it is Example or a subclass). |
|---|
| 19 | |
|---|
| 20 | Now both C{Example.test} and C{Example().test} invoke the getter. |
|---|
| 21 | A "normal" property only works on instances. |
|---|
| 22 | """ |
|---|
| 23 | |
|---|
| 24 | def __init__(self, getter): |
|---|
| 25 | self.getter = getter |
|---|
| 26 | |
|---|
| 27 | def __get__(self, instance, owner): |
|---|
| 28 | return self.getter(owner) |
|---|