Skip to content

Instantly share code, notes, and snippets.

@capttwinky
Created September 27, 2011 17:51
Show Gist options
  • Save capttwinky/1245740 to your computer and use it in GitHub Desktop.
Save capttwinky/1245740 to your computer and use it in GitHub Desktop.
example of adding sort functins to an object & using in a lambda
import time
class myNote(object):
def __init__(self, myInt):
self.name = myInt
self.provided_time = None
self.mytime = time.time()
#we're going to consider every thing above here imported
#first we assign a property to our imported object
@property
def mytime(self):
return self.provided_time or self.mytime
myNote.time = mytime
#construct a list of objects
myNotes = [myNote(i) for i in range(10)]
#show the list before mutation
print[note.name for note in myNotes]
#set the provided_time in some of the onjects
for myI in range(10,0,-2):
print myI-1
myNotes[myI-1].provided_time = time.time()
time.sleep(.5)
#sort the list, use a lambda to call the note.time
myNotes.sort(key=lambda note: note.time, reverse=True)
#print the mutated and sorted list
print[note.name for note in myNotes]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment