Implement logic on object.attribute and object.attribute()
Chris Angelico
rosuav at gmail.com
Sun Nov 24 09:37:12 EST 2013
On Mon, Nov 25, 2013 at 1:16 AM, Marc Aymerich <glicerinu at gmail.com> wrote:
> ... def do_get(self):
> ... # Do a HTTP GET request.
> ... return "Get stuff"
> ... def do_put(self):
> ... # Do a HTTP PUT request.
> ... return "Put stuff"
To make this a bit more realistic, try this instead - tying in with
what I said in response to Roy:
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.
print("Doing the GET call, please wait...")
time.sleep(1)
return "Get stuff"
def do_put(self):
# Do a HTTP PUT request.
print("Doing the PUT call, please wait...")
time.sleep(1)
return "Put stuff"
(PUT or POST, makes no difference; I think you were looking for POST,
but Steven wrote PUT here so I'll use that for simplicity)
>>> Magic_HTTP_Thing().attribute()
Doing the GET call, please wait...
Doing the PUT call, please wait...
'Put stuff'
And that's what you don't want happening. Your PUT / POST calls are
going to take twice as long as they should.
ChrisA
More information about the Python-list
mailing list