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

Marc Aymerich glicerinu at gmail.com
Sun Nov 24 13:52:51 CET 2013


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!!
-- 
Marc



More information about the Python-list mailing list