0

I'm try to learn OOP in Python. The code below gives you a better idea of what I'm doing. I want to return an object what allows me to call other methods on that data. Is this the right way?

content = HTTP().GET(resource="photo/2/")
content.get_image()

Class

class HTTP(object):

    def __init__(self):
        """
        Creates a new instance of the class and assigns local variables.
        """
        self._resource = None
        self._payload = None
        self._response = None


    @property
    def resource(self):
        return self._resource

    @resource.setter
    def resource(self, value):
        self._resource = "http://api.test.com/" % value


    @property
    def payload(self):
        return self._payload

    @payload.setter
    def payload(self, value):
        self._payload = value


    @property
    def response(self):
        return self._response

    @response.setter
    def response(self, value):
        self._response = value


    def GET(self, resource):
        """
        Sends a GET request. Returns :class:`Response` object.
        :param resource: URL for the new :class:`Request` object.
        """
        self.resource = resource
        self.response = requests.get(self.resource).json()
        return self


    def get_image(self):
        """
        Gets raw image from response.
        :return: image
        """
        return requests.get(self.response["raw"])

Later I may want to extend this and do

content = HTTP().POST(resource="photo/2/", payload='{"somekye":"somevalue"}')

or even:

content = HTTP().GET(resource="photo/2/")
content.POST(payload='{"somekye":"somevalue"}')
3
  • It's the right way, but you should probably return self.response instead of self if you want to work on the response otherwise you have to access it like content.response.get_image(). Commented Aug 8, 2014 at 9:59
  • Check out requests for a well-done implementation of a HTTP library. Commented Aug 8, 2014 at 9:59
  • @Roland Smith try to ignore the requests part this is more about my class and OOP Commented Aug 8, 2014 at 10:01

2 Answers 2

1

You don't have to do it this way. You can just modify the 'resource' field, and then simply operate on your instance of HTTP object. Like this:

content = HTTP()
content.resource(valueToSet)
content.response(valueToSet)

And that's it.

Sign up to request clarification or add additional context in comments.

2 Comments

A lot of examples I see init the values first, which way is better?
You mean, in the constructor? Yes, it's equivalent to content = HTTP(resource, response) if you change the constructor accordingly (if I understood what you meant)
0

If you want to be able to read and modify an attribute, there is generally no need for getter and setter methods.

A good reason to use properties is e.g. if you need to check the incoming values and possibly raise an exception.

But in that case I would propose you use a method, so that it is obvious to the user that you are executing some code. Because you wouldn't expect an exception to occur when modiying an attribute.

If you want to prevent properties from modification, you should use a metaclass.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.