1,105,427 Community Members

writing get & set method

Member Avatar
krystosan
Junior Poster
191 posts since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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)
Member Avatar
krystosan
Junior Poster
191 posts since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
woooee
Posting Maven
2,793 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: