Simple questions on use of objects (probably faq)
James Stroud
jstroud at ucla.edu
Wed Mar 8 15:35:48 EST 2006
Brian Elmegaard wrote:
> "Matt Hammond" <matt.hammond at rd.bbc.co.uk> writes:
>
>
>>y_max = max([e.x for e in y])
>
>
> Would there be a way to refer back to the e with maximum x, or how
> could I find other attributes of it?
>
You should look into __cmp__ and other magic methods. This is probably
the type of functionality you seem to be after.
class C:
def __init__(self, x):
self.x = x
def __repr__(self):
idnum = str(id(self))[-4:] #
return "C(x=%s):%s" % (self.x, idnum) # for demo
def __cmp__(self, other):
return self.x - other.x
# demonstration
import random
sees = [C(x) for x in (4,7,1,3,0,9,2)] # random-ish ints
print sees
print max(sees)
sees.sort()
print sees
James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com/
More information about the Python-list
mailing list