Sahand
Sahand

Reputation: 8370

Python: How to use method with decorator properly

I have a Message class that I would like to set the payload of. However, I cannot figure out how to use the proper method for doing this. This is how the Message class looks in the source code (Removed irrelevant parts):

class Message(object):
    def __init__(self):
        self._type = None
        self._mid = None
        self._token = None
        self._options = []
        self._payload = None
        self._destination = None
        self._source = None
        self._code = None
        self._acknowledged = None
        self._rejected = None
        self._timeouted = None
        self._cancelled = None
        self._duplicated = None
        self._timestamp = None
        self._version = 1

    @property
    def payload(self):
        """
        Return the payload.

        :return: the payload
        """
        return self._payload

    @payload.setter
    def payload(self, value):
        """
        Sets the payload of the message and eventually the Content-Type

        :param value: the payload
        """
        if isinstance(value, tuple):
            content_type, payload = value
            self.content_type = content_type
            self._payload = payload
        else:
            self._payload = value

If I try to set the payload with messageObject.payload("Hello World") I get the error: TypeError: 'NoneType' object is not callable.

What is the proper way to set the payload?

Upvotes: 0

Views: 85

Answers (1)

Dhia
Dhia

Reputation: 10619

When you use the property decorator, payload is not any more used as a method, it becomes property as indicates the decorator name and used as follow:

message_object.payload = "Hello World" # set the payload property
message_object.payload # get the payload property

Upvotes: 1

Related Questions