# A Universe Set

Discussion in 'Python' started by jordan.nick@gmail.com, Oct 4, 2006.

1. ### Guest

Has the addition of a Universe Set object ever been suggested. Like U
= set(0), so that any object was a member of U? Maybe this gets into
some crazy Cantorian stuff since U is in U. But it seems like it would
be useful and would have a nice symmetry with emptyset:set([]), that
is:

for any object a:
"a in set([])" returns False
"a in set(0)" returns True

, Oct 4, 2006

2. ### Robert KernGuest

wrote:
> Has the addition of a Universe Set object ever been suggested. Like U
> = set(0), so that any object was a member of U?

In [61]: class UniverseSet(object):
....: def __contains__(self, x):
....: return True
....:

In [62]: U = UniverseSet()

In [63]: 1 in U
Out[63]: True

In [64]: 2 in U
Out[64]: True

In [65]: 'something' in U
Out[65]: True

In [66]: U in U
Out[66]: True

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
an underlying truth."
-- Umberto Eco

Robert Kern, Oct 4, 2006

3. ### Steve HoldenGuest

wrote:
> Has the addition of a Universe Set object ever been suggested. Like U
> = set(0), so that any object was a member of U? Maybe this gets into
> some crazy Cantorian stuff since U is in U. But it seems like it would
> be useful and would have a nice symmetry with emptyset:set([]), that
> is:
>
> for any object a:
> "a in set([])" returns False
> "a in set(0)" returns True
>

>>> class universe(object):

... def __contains__(self, thing):
... return True
...
>>> U = universe()
>>> 1 in U

True
>>> None in U

True
>>> U in U

True
>>>

Of course the last eexample shows that your implementation may need to
change depending on how you view Russell's paradox ...

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Steve Holden, Oct 4, 2006
4. ### Jorgen GrahnGuest

On Tue, 03 Oct 2006 23:13:08 -0500, Robert Kern <> wrote:
> wrote:
>> Has the addition of a Universe Set object ever been suggested. Like U
>> = set(0), so that any object was a member of U?

>
> In [61]: class UniverseSet(object):
> ....: def __contains__(self, x):
> ....: return True
> ....:

Yes. But note that being able to write it yourself is one thing, having it
in the Standard Library and known to anyone is another.

I have been craving for some similar things for a while, and I'm still not
sure if they are good ideas, or brain damage caused by studying functional
programming at Uni. For example:

- the wildcard object, which compares equal to everything else
- infinite xrange()s
- the black hole function 'def f(*args): pass'
- the identity function 'def f(x): return x'

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!

Jorgen Grahn, Oct 4, 2006
5. ### Leif K-BrooksGuest

Jorgen Grahn wrote:
> - infinite xrange()s

itertools.count()?

Leif K-Brooks, Oct 4, 2006
6. ### MonkeeSageGuest

Jorgen Grahn wrote:
> - infinite xrange()s

Fun. How about in-memory objects which use no memory, and
self-referential anonymous functions, and object states without
objects...

Regards,
Jordan

MonkeeSage, Oct 4, 2006
7. ### Paul McGuireGuest

Paul McGuire, Oct 4, 2006
8. ### Istvan AlbertGuest

Jorgen Grahn wrote:

> I have been craving for some similar things for a while, and I'm still not
> sure if they are good ideas, or brain damage caused by studying functional
> programming at Uni.

This is a self correcting situation, as ayone who understands why they
need this can surely write them using itertools and some simple
classes.

No Child Left Behind.

Istvan Albert, Oct 4, 2006
9. ### Wildemar WildenburgerGuest

Jorgen Grahn wrote:
> - the wildcard object, which compares equal to everything else
> - infinite xrange()s
> - the black hole function 'def f(*args): pass'
> - the identity function 'def f(x): return x'

Any use cases for these?

Wildemar Wildenburger, Oct 4, 2006
10. ### Duncan BoothGuest

Wildemar Wildenburger <> wrote:

> Jorgen Grahn wrote:
>> - the wildcard object, which compares equal to everything else
>> - infinite xrange()s
>> - the black hole function 'def f(*args): pass'
>> - the identity function 'def f(x): return x'

>
> Any use cases for these?
>

I guess the first one could be useful if you want to compare two data
structures but prune part of the structure in the comparison (e.g. in a
unit test assertion). So not forgetting that this only works when the
wildcard is on the left of the comparison:

>>> class DontCareClass(object):

def __eq__(self, other):
return True

>>> dontcare = DontCareClass()
>>> [1, dontcare, 3]==[1, 2, 3]

True
>>> [1, dontcare, 3]==[1, 4, 3]

True
>>> [1, dontcare, 3]==[1, 4, 2]

False

I think for more general use though it's a non-starter. If Python had it
builtin it should compare equal in *all* situations and what on earth

I can think of one use for a black hole function, but it is very specific.
It would be kind of nice to have a variation on 'super' which traps
AttributeError and returns the black hole instead, or perhaps just an
optional third argument for a default to return (c.f. dict.get). So you
could write:

class C(object):
def method(self, arg):
super(C, self, blackhole).method(arg)
... whatever ...

and method would call any base class definition of method but not care if
there isn't one. The alternative to this is either to catch and ignore the
exception (which is a pain), or better to create a dummy base class which
implements the interface but doesn't propagate the calls.

I suppose the same argument could be made for any pattern of calling the
result of getattr when the attribute might not exist. Replace:

m = getattr(obj, key, None)
if m is not None:
m(something)
or:

if hasattr(obj, 'somekey'):
obj.somekey(whatever)

with:

getattr(obj, key, blackhole)(something)

The catch here is that the current pattern often has an 'else' clause as
well and you can't replace those ones with blackhole.

Duncan Booth, Oct 5, 2006
11. ### Jorgen GrahnGuest

On Wed, 04 Oct 2006 11:00:28 -0400, Leif K-Brooks <> wrote:
> Jorgen Grahn wrote:
>> - infinite xrange()s

>
> itertools.count()?

Oops! You're right. The itertools documentation even refers to the SML and
Haskell languages. And it contains itertools.izip(), another thing on my
wish list.

I have reasons to be Python 2.2 compatible, so I haven't really looked
through the nice things in 2.3 and up (generators are in __future__ in 2.2).
Stupid of me to overlook itertools, which I've read about here many times.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!

Jorgen Grahn, Oct 5, 2006
12. ### Jorgen GrahnGuest

On Thu, 05 Oct 2006 00:02:51 +0200, Wildemar Wildenburger <> wrote:
> Jorgen Grahn wrote:

> Any use cases for these?

>> - the wildcard object, which compares equal to everything else

Like someone else wrote, for quick-and-dirty comparisons or lists and
dictionaries where I don't care about one part. I think I wanted it for
unittest's assertEqual(foo, bar) at one point.

self.assertEqual(['foo', 42, [], WILDCARD], my_result)

versus

self.assertEqual('foo', my_result[0])
self.assertEqual(42, my_result[1])
self.assertEqual([], my_result[2])
self.assertEqual(4, len(my_result))
# possibly assert that 'my_result' is a list-like
# object too

But I agree that the WILDCARD isn't the kind of object you want to spread
throughout your code; its behaviour is too odd.

>> - infinite xrange()s

Available in itertools, as someone pointed out.

>> - the black hole function 'def f(*args): pass'

I often find myself adding logging to functions by passing sys.stderr.write
as an argument to it. Passing blackhole is an elegant and fast way of
disabling logging.

>> - the identity function 'def f(x): return x'

I don't think I've used it. Maybe if you do a lot of manipulation of
functions and functors -- in some sense it's to function application what 0
is to addition, or 1 to multiplication.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!

Jorgen Grahn, Oct 5, 2006
13. ### hanumizzleGuest

On 10/4/06, Wildemar Wildenburger <> wrote:
> Jorgen Grahn wrote:
> > - the wildcard object, which compares equal to everything else
> > - infinite xrange()s
> > - the black hole function 'def f(*args): pass'
> > - the identity function 'def f(x): return x'

>
> Any use cases for these?

I used the last one in my .emacs (but it had a lot to with stupid
emacs lisp). It might also be useful for some theoretical aspects of
FP and mathematical applications (maybe! IANAE!!). See:
http://en.wikipedia.org/wiki/Fixed_point_(mathematics)

-- Theerasak

hanumizzle, Oct 6, 2006
14. ### Paul McGuireGuest

> - the wildcard object, which compares equal to everything else

class MatchAny(object):
def __cmp__(self,other):
return 0

wild = MatchAny()

print wild == 1000
print 1000 == wild
print wild == (1,2,3)
print wild == 'abck'
print wild == wild

print wild != 1000
print 1000 != wild
print wild != (1,2,3)
print wild != 'abck'
print wild != wild

Prints:
True
True
True
True
True
False
False
False
False
False

Paul McGuire, Oct 16, 2006
15. ### Paul McGuireGuest

"Jorgen Grahn" <> wrote in message
news:...
> Yes. But note that being able to write it yourself is one thing, having it
> in the Standard Library and known to anyone is another.
>

Perhaps you could compile the submissions in this thread into an entry in
the Python Cookbook. Probably the next best thing to being in the stdlib,
and it might even make it into the next print edition (earning you a free
copy!).

-- Paul

Paul McGuire, Oct 16, 2006
16. ### Guest

>> - the wildcard object, which compares equal to everything else

Paul> class MatchAny(object):
Paul> def __cmp__(self,other):
Paul> return 0

Paul> wild = MatchAny()

...

You're at the mercy of the comparison machinery implemented by individual
classes. Executing this script (using any of Python 2.3 through what's
currently in the SVN repository):

import datetime

class MatchAny(object):
def __cmp__(self,other):
return 0

wild = MatchAny()

print wild == datetime.datetime.now()
print datetime.datetime.now() == wild
print wild != datetime.datetime.now()
print datetime.datetime.now() != wild

yields

False
False
True
True

Skip

, Oct 16, 2006
17. ### Simon BrunningGuest

On 10/16/06, <> wrote:
>
> You're at the mercy of the comparison machinery implemented by individual
> classes.

Plus, if you put a wildcard object into a set (or use it as a
dictionary key) you'll confuse yourself horribly.

I know I did. ;-)

--
Cheers,
Simon B

http://www.brunningonline.net/simon/blog/

Simon Brunning, Oct 16, 2006
18. ### Jorgen GrahnGuest

On Mon, 16 Oct 2006 09:21:11 -0500, <> wrote:
>

[me]
> >> - the wildcard object, which compares equal to everything else

>

[Paul]
> Paul> class MatchAny(object):
> Paul> def __cmp__(self,other):
> Paul> return 0
>
> Paul> wild = MatchAny()

FWIW, I define __eq__ in the one I (infrequently) use.

> ...
>
> You're at the mercy of the comparison machinery implemented by individual
> classes. Executing this script (using any of Python 2.3 through what's
> currently in the SVN repository):

....

Oh. /Is/ there a way of making it work, then? If we ignore the problems
with having such objects in the first place, that is.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!

Jorgen Grahn, Oct 17, 2006
19. ### Duncan BoothGuest

Jorgen Grahn <> wrote:

>> >> - the wildcard object, which compares equal to everything else

>>

> [Paul]
>> Paul> class MatchAny(object):
>> Paul> def __cmp__(self,other):
>> Paul> return 0
>>
>> Paul> wild = MatchAny()

>
> FWIW, I define __eq__ in the one I (infrequently) use.

The advantage to using __eq__ is that it fails to work correctly less often
than __cmp__. Compare skip's example where comparison against datetime give
the wrong answer 100% of the time with __cmp__ and 50% of the time with
__eq__/__ne__:

>>> import datetime
>>> class MatchAny(object):

def __cmp__(self,other):
return 0

>>> wild = MatchAny()
>>> print wild == datetime.datetime.now()

False
>>> print datetime.datetime.now() == wild

False
>>> print wild != datetime.datetime.now()

True
>>> print datetime.datetime.now() != wild

True
>>> class MatchAny(object):

def __eq__(self,other):
return True
def __ne__(self,other):
return False

>>> wild = MatchAny()
>>> print wild == datetime.datetime.now()

True
>>> print datetime.datetime.now() == wild

False
>>> print wild != datetime.datetime.now()

False
>>> print datetime.datetime.now() != wild

True

>
>> ...
>>
>> You're at the mercy of the comparison machinery implemented by
>> individual classes. Executing this script (using any of Python 2.3
>> through what's currently in the SVN repository):

> ...
>
> Oh. /Is/ there a way of making it work, then? If we ignore the
> problems with having such objects in the first place, that is.
>

In a contained environment, it can be made to work 'well enough'. So if you
are writing unit tests and want a wildcard element to include inside data
structures, AND you can guarantee that you always assert in the order
expected,actual, then you can probably get away with it. That's assuming
you remembered to use __eq__/__ne__ rather than __cmp__.

For an object safe to let out in the wild though there isn't any way to
implement it.

Duncan Booth, Oct 17, 2006