Python Forum
Can property getters and setters have additional arguments?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can property getters and setters have additional arguments?
#1
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).

The code is as follows:

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)))
And this is the output I get:

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'
Please tell me what I am doing wrong here.

Peter
Reply
#2
Not sure what are you trying to achieve, but please execute the following code:
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('This is what is created inside Matrix object: ')
print(type(mygal.visited))
print(mygal.visited)
print('This is how mycoord looks like: ')
print(type(mycoord))
print(mycoord)
Attribute visited is very strange to me.
Reply
#3
I added your code and reduced the Matrix size from 10 to to 3 so output would be shorter. Below is the output.

The arrays in the class and the Coord() value are NUMPY arrays, not regular python arrays.

My question is still whether I am off-base in thinking that property methods and getter/setters can take additional positional parameters as in my example.

Peter

Output:
mycoord=[1 2 3] This is what is created inside Matrix object: <class 'numpy.ndarray'> [[[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]] This is how mycoord looks like: <class 'numpy.ndarray'> [1 2 3]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,686 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,565 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,256 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Property price calculation oli_action 4 3,093 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Use of @property decorator ruy 16 6,418 Jun-09-2020, 05:29 PM
Last Post: buran
  Additional buffer for Windows constantin01 0 1,363 Mar-31-2020, 10:24 AM
Last Post: constantin01
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,019 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 2,313 Nov-25-2019, 04:32 PM
Last Post: KaliLinux
  How to write a script to execute a program need passing additional input? larkypython 2 2,472 Nov-23-2019, 04:38 AM
Last Post: larkypython
  print all method and property of list object engmoh 4 2,802 Oct-26-2019, 05:33 PM
Last Post: engmoh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020