Created
September 27, 2011 17:51
-
-
Save capttwinky/1245740 to your computer and use it in GitHub Desktop.
example of adding sort functins to an object & using in a lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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