Created
April 8, 2016 18:30
-
-
Save kezabelle/43f45edefdcd1377b70eb171d4cffd1e to your computer and use it in GitHub Desktop.
for perrygeo on reddit in thread https://www.reddit.com/r/Python/comments/4dwll7/decorator_to_treat_method_as_a_property_or/
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
from __future__ import unicode_literals | |
from wrapt import ObjectProxy | |
class Callable(ObjectProxy): | |
def __call__(self, *args, **kwargs): | |
result = self.__wrapped__ | |
if 'upper' in kwargs: | |
result = result.upper() | |
return result | |
class Test(object): | |
__slots__ = ('name',) | |
def __init__(self, name): | |
self.name = name | |
@property | |
def fullname(self): | |
return Callable(self.name) | |
test = Test(name='test') | |
assert test.fullname == 'test' | |
assert isinstance(test.fullname, unicode) is True | |
assert test.fullname(upper=True) == 'TEST' | |
assert test.fullname()[3] == 't' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment