How do you pass a standard operator such as '<' as a parameter?

Peter Milliken peterm at resmed.com.au
Thu Nov 20 18:20:36 EST 2003


Thanks :-)

I'll file that one away for future reference.


"Robert Brewer" <fumanchu at amor.org> wrote in message
news:mailman.942.1069364085.702.python-list at python.org...
Don't forget the 'operators' module:

http://www.python.org/doc/current/lib/module-operator.html


FuManChu

> -----Original Message-----
> From: Peter Milliken [mailto:peterm at resmed.com.au]
> Sent: Thursday, November 20, 2003 1:22 PM
> To: python-list at python.org
> Subject: Re: How do you pass a standard operator such as '<'
> as a parameter?
>
>
> Wrote too soon - the obvious answer is not to use a
> "comparison" function at
> all when instantiating the class but rather to just code the
> "<" in the
> class "add" procedure and then make sure the data type that
> is being used
> contains a __lt__ operator :-) i.e. the class definition
> should just be:
>
>         class OrderedList (list):
>           def add (self, element):
>             """Add the element into the list in the correct
> ordered sequence
> for
>             the data type.
>             """
>             # Locate the position in the list and perform the
> insertion
>             for i in list(self):
>               if element < i:
>                 list.insert(self, list.index(self, i), element)
>                 break
>             else:
>               # element is greater than any current value in
> the list, so
> stick
>               # it at the end.
>               list.append(self, element)
>
> This will work for integers as well as user defined data
> types that have a
> __lt__ function defined.
>
>
>
> "Peter Milliken" <peterm at resmed.com.au> wrote in message
> news:z7avb.541$WD1.13194 at nnrp1.ozemail.com.au...
> > I am creating a subclass of list that will allow 'ordered'
> lists via the
> > addition of a new method ('add' for want of a better name
> :-)). Since I
> want
> > to make it as generic as possible, I want to pass the
> comparator function
> as
> > an argument at initialisation. It would be used line this:
> >
> > y = OrderedList([], LessThanFunction)
> >
> > y.add(9)
> > y.add(8)
> > y.add(11)
> >
> > y
> >
> > [8,9,11]
> >
> >
> >
> > So the class definition would look like this:
> >
> >         class OrderedList (list):
> >           def __init__ (self, comparator):
> >             list.__init__(self)
> >             self.ComparisonFunction = comparator
> >
> >           def add (self, element):
> >             """Add the element into the list in the correct ordered
> sequence
> > for
> >             the data type.
> >             """
> >             # Locate the position in the list and perform
> the insertion
> >             for i in list(self):
> >               if self.ComparisonFunction(element, i):
> >                 list.insert(self, list.index(self, i), element)
> >                 break
> >             else:
> >               # element is greater than any current value
> in the list, so
> > stick
> >               # it at the end.
> >               list.append(self, element)
> >
> >
> > For non-standard data types, you would obviously define a
> '<' function and
> > then pass it as a parameter at initialisation, but how can
> you pass one of
> > the standard operators? i.e. '<'.
> >
> > For instance, to create an ordered list of integers, I would like to
> > instantiate new objects using something like this:
> >
> >   y = OrderedList([], <)
> >
> > However, this results in a syntax error. Is there anyway to
> pass the '<'
> > operator itself? I fully realise that you could create a "lessthan"
> > function, either explicitly or as a lamdba, but my
> curiousity bump is
> > itching and I would like to know how to pass one of the standard
> operators.
> >
> > Thanks
> > Peter
> >
> >
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>






More information about the Python-list mailing list