Characters contain themselves?

Graham Fawcett graham.fawcett at gmail.com
Mon Apr 10 13:23:54 EDT 2006


WENDUM Denis 47.76.11 (agent) wrote:
> While testing recursive algoritms dealing with generic lists I stumbled
> on infinite loops which were triggered by the fact that (at least for my
> version of Pyton) characters contain themselves.
[snip]
> Leading to paradoxes and loops objects which contain themselves (and
> other kinds of monsters) are killed in set theory with the Axiom of
> Foundation:=)

You could always use an "is-proper-subset-of" function, which is closer
to the intent of your algorithm. Using Jamitzky's very clever infix
recipe [1], you can even write it as an infix operator:

#Jamitzky's infix-operator class, abbreviated
class Infix:
    def __init__(self, function):
        self.function = function
    def __ror__(self, other):
        return Infix(lambda x, self=self, other=other:
self.function(other, x))
    def __or__(self, other):
        return self.function(other)
    def __call__(self, value1, value2):
        return self.function(value1, value2)

# define our is-proper-subset operator...
ips = Infix(lambda a, b: (a is not b) and (a in b))

>>> print 'a' |ips| 'a'
False
>>> print 'a' |ips| 'abc'
True
>>> print '' |ips| ''
False

Of course, |ips| looks like Lips-L, which is odd; but you could pick a
better name. ;-)

Graham

[1] http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/384122




More information about the Python-list mailing list