Implement logic on object.attribute and object.attribute()
Marc Aymerich
glicerinu at gmail.com
Sun Nov 24 09:16:38 EST 2013
On Sun, Nov 24, 2013 at 2:45 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Sun, 24 Nov 2013 05:04:16 -0800, Devin Jeanpierre wrote:
>
>> On Sun, Nov 24, 2013 at 4:52 AM, Marc Aymerich <glicerinu at gmail.com>
>> wrote:
>>> Hi,
>>> I'm playing with python internals to make objects behave like this:
>>>
>>> if I access to "object.attribute" I want to return the result of an
>>> HTTP GET request. However if i call "object.attribute()" I want an HTTP
>>> POST request to be executed.
>>
>> Uh oh. What you want is impossible. You cannot call an attribute without
>> first accessing it. :(
>
> Not quite impossible. All you need is an object that behaves like a
> string, except it has a __call__ method. Here's a sketch of a solution,
> completely untested.
>
>
> class CallableString(str):
> # Like a string, but callable.
> def function(self):
> raise NotImplementedError(
> "this must be overridden on the instance"
> )
> def __call__(self):
> return self.function()
>
>
> class Magic_HTTP_Thing:
> @property
> def attribute(self):
> result = CallableStr(self.do_get())
> result.function = lambda: self.do_put()
> def do_get(self):
> # Do a HTTP GET request.
> return "Get stuff"
> def do_put(self):
> # Do a HTTP PUT request.
> return "Put stuff"
OMG steven, it actually works :)
>>> class CallableString(str):
... # Like a string, but callable.
... def function(self):
... raise NotImplementedError(
... "this must be overridden on the instance"
... )
... def __call__(self):
... return self.function()
...
>>>
>>> class Magic_HTTP_Thing:
... @property
... def attribute(self):
... result = CallableString(self.do_get())
... result.function = lambda: self.do_put()
... return result
... def do_get(self):
... # Do a HTTP GET request.
... return "Get stuff"
... def do_put(self):
... # Do a HTTP PUT request.
... return "Put stuff"
...
>>>
>>> Magic_HTTP_Thing().attribute
'Get stuff'
>>> Magic_HTTP_Thing().attribute()
'Put stuff'
>
> Possible or not, it doesn't seem like a reasonable API to me.
yeah, this is a "corner case" of our REST API, I have some badly
design endpoints that mostly behave like functions, but some of them
also contain state information that you can GET, I was trying to map
this behavior to python objects and this interface is the best that
occurred to me :)
--
Marc
More information about the Python-list
mailing list