[Python 3.4] Rationale for readonly slice data attributes

Mario Figueiredo marfig at gmail.com
Thu Mar 5 18:57:29 EST 2015


What is the rationale behind making the slice class data attributes
readonly?

I've built a __getitem__ method for a Map class that contains a list
of Cell instance objects. __getitem__ maps this list into a matrix::

    # get cell at cartesian coordinates 12, 4
    # will map to the 1048th position in the cells list
	mmap = Map()
    print(mmap[12, 4])

All is working well and it even supports slicing::

	# return all cells with x between 3 and 6 and y up until 4
	mmap = Map()
    [print(cell) for cell in mmap(3:6, :4)]

This last example is one case in which I missed the ability to change
the the `start` and `stop` attributes of the slice class in order for
the maping to occur that allows a 2-dimensions access to a 1-dimension
list.

It was solved by taking another turn, but it proved to me that the
slice data attributes are more useful if they are writable, and this
clashes tremendously with the principles of data encapsulation that
underline the requirements for a readonly property. In other words, if
it needs to be accessed, don't hide it. If it needs to be written,
don't lock it.

Now, normally I take these things for granted. Rules are rules and
their reasons are often fairly obvious. But in this case I'm at loss.
I can't find a reason for this class attributes to have been made into
readonly properties.



More information about the Python-list mailing list