I wrote this function earlier to set the priority if the items in listwidget, however what I think would be more pythonic is to write get and set priority method...

how should i proceed with writing the method in a pythonic way ?

def changePriority(self,direction):
    """ Reorder the items in the listWidget """
    crntRow = newrow = self.listWidget.currentRow()
    total   = self.listWidget.count()
    self.statusbar.showMessage("Total no. of items %s, and selected item number is %s"%(total,crntRow),2500)
    if direction == 'up':
        if crntRow > 0 : newrow -= 1
        else: self.statusbar.showMessage("This is the first item cannot move up further.",1500)
    elif direction == 'down':
        if crntRow + 1  < total: newrow += 1
        else: self.statusbar.showMessage("This is the last item cannot move down further.",1500)
    if crntRow != newrow:
        crntItem=self.listWidget.takeItem(crntRow)
        self.listWidget.insertItem(newrow,crntItem)
        self.listWidget.setCurrentItem(crntItem)

I have tried to write here is what I have so far, don't understand how will i put in get and what probably the current row ?

class Priority(QtGui.QtMainWindow):

    def __init__(self,direction):
        self.__direction = direction
        crntRow = newrow = self.listWidget.currentRow()
        total   = self.listWidget.count()

    def getPriorty(self):

        pass

    def setPriority(self):

        pass

lines 5 and 6 have no effect as they set local variables and do not use them. Setters and getters are not considered very pythonic. Prefered way is to use attributes and replace those with calculated properties only after need arrices. However it is common that teachers of courses require those methods.

As Tony said getters and setters are not used in Python as it is inefficient to create a function just to change or return some variable when that can be done directly as the following example illustrates

class Priority():

    def __init__(self, priority):
        self.priority = priority

Pr = Priority(1)
print Pr.priority     ## getter
Pr.priority = 9       ## setter
print Pr.priority     ## getter

There are many discussions on the web like this one if you want to read more about it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.