Implement logic on object.attribute and object.attribute()

M

Marc Aymerich

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.

So far I have been able to do the POST part, using two classes like this:

class HTTPAttribute(object):
def __init__(self, name):
self.name = name

def __call__(self, *args, **kwargs):
url = BASE_URL + self.name
requests.post(url, *args, **kwargs)


class HTTPObject(object):
def __getattr__(self, name):
return HTTPAttribute(name)


But I'm stuck implementing the HTTP GET request when accessing
"object.attribute", Any idea ??

Thanks!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top