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,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top