Can property getters and setters have additional arguments?

pjfarley3 at earthlink.net pjfarley3 at earthlink.net
Sat Oct 31 10:54:17 EDT 2020


Apologies if you receive two copies of this message, but I sent it almost 10
hours ago and still have not seen it come back to me or appear in the
archives.

I would appreciate any help or RTFM you can provide.

Peter

> -----Original Message-----
> From: Peter Farley
> Sent: Saturday, October 31, 2020 1:17 AM
> To: 'python-list at python.org' <python-list at python.org>
> Subject: Can property getters and setters have additional arguments?
> 
> I asked this question over on the python-forum-io group but haven't gotten
> much in the way of answers.  I hope someone here can advise me.
> 
> I am trying to implement getters and setters in a class using a numpy
array as the
> base instance value, but I am not able to pass an array index argument to
the
> getter function (and probably not to the setter function, but it never
gets that
> far).
> 
> This is all happening on a Win10x64 system using python 3.8.5.
> 
> Please help me understand whether what I tried to code here is even
possible.
> 
> Peter
> 
> The code is as follows:
> 
> ---- cut here ----
> import numpy as np
> 
> class Matrix:
>     def __init__(self, msize):
>         self.msize = msize
>         self.mvalues = np.full([msize + 2, msize + 2, msize + 2], -1,
>                                dtype=np.int32)
>         self.mvalues[1:-1, 1:-1, 1:-1] = 0
>         self.visited = np.zeros([msize + 2, msize + 2, msize + 2],
>                                 dtype=np.int32)
> 
>     @property
>     def visits(self, coord):
>         return self.visited[coord[0], coord[1], coord[2]]
> 
>     @visits.setter
>     def incr_visits(self, coord, incr):
>         self.visited[coord[0], coord[1], coord[2]] += incr
> 
> 
> def Coord(x, y, z):
>     return np.full([3,], (x, y, z), dtype=np.int32)
> 
> mycoord = Coord(1, 2, 3)
> print("mycoord={}".format(mycoord))
> 
> mygal = Matrix(10)
> print("Before set:{}".format(mygal.visits(mycoord)))
> 
> mygal.incr_visits(mycoord, 10)
> print("After  set:{}".format(mygal.visits(mycoord)))
> ---- cut here ----
> 
> The output I get is as follows:
> 
> ---- output ----
> mycoord=[1 2 3]
> Traceback (most recent call last):
>   File "C:\Users\MyUser\Test\clstest4.py", line 28, in <module>
>     print("Before set:{}".format(mygal.visits(mycoord)))
> TypeError: visits() missing 1 required positional argument: 'coord'
> ---- output ----
> 




More information about the Python-list mailing list