Search code examples
pythongetter-setterpython-decoratorspython-class

How to use @property decorator to restrict values inside a list


I am a newbie in python. So if I write something wrong, feel free to correct.

I have made a new class say ClassA which has an attribute modified, which is a list.

The class initially looked like this:

class ClassA:
        def __init__(self):
            self.modified = []

I want that the elements which are added (or modified) in this list pass through a check first. For eg. I want only dates inside this list which are of the form YYYY-MM-DD. So I thought that @propoerty decorator can do some work. I used it in the following way:

class ClassA:

        def __init__(self):
            self._modified = []

        @property
        def modified(self):
            return getattr(self, "_modified")

        @modified.setter
        def modified(self, value):
            if not isinstance(value, list):
                raise TypeError("value passed must be a list")
            else:
                modified_list = []
                for item in value:
                    if isinstance(item, str):
                        result = performValidationCheck(item)
                        if not result:
                            raise ValueError("date format incorrect")
                        else:
                            modified_list.append(date.getDateAsString())
                    else:
                        raise TypeError("%s must be of type string")
                self._modified = modified_list

        def add_modified_date(self, date):
            if not isinstance(date, str):
                raise TypeError("date passed must be a string")
            result = performValidationCheck(date)
            if not result:
                raise ValueError(libsbml.OperationReturnValue_toString(result))
            self._modified.append(date)

However, I am still able to change the values of entries inside the modified list by using classA.modified[index]. Even the append function is also working on this list.

I have implemented the performValidationCheck() function, that's not the concern here.

Can someone help me with how should I proceed with this?


Solution

  • However, I am still able to change the values of entries inside the modified list by using classA.modified[index]. Even the append function is also working on this list.However, I am still able to change the values of entries inside the modified list by using classA.modified[index]. Even the append function is also working on this list.

    Of course. @property only controls getting / setting the attribute on the object. If the object set on the attribute is mutable it can't do anything.

    Can someone help me with how should I proceed with this?

    Either use a custom list-like collection (a MutableSequence) which validates its values on the fly, or use a tuple (basically an immutable list) instead, this way callers will not be able to modify it in-place.