Velocity Reviews > A Universe Set

# A Universe Set

jordan.nick@gmail.com
Guest
Posts: n/a

 10-04-2006
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

Robert Kern
Guest
Posts: n/a

 10-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

Steve Holden
Guest
Posts: n/a

 10-04-2006
(E-Mail Removed) 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

Jorgen Grahn
Guest
Posts: n/a

 10-04-2006
On Tue, 03 Oct 2006 23:13:08 -0500, Robert Kern <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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!

Leif K-Brooks
Guest
Posts: n/a

 10-04-2006
Jorgen Grahn wrote:
> - infinite xrange()s

itertools.count()?

MonkeeSage
Guest
Posts: n/a

 10-04-2006
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

Paul McGuire
Guest
Posts: n/a

 10-04-2006
"Jorgen Grahn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..

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

Also not so farfetched.

See the disable and enable decorators at
http://wiki.python.org/moin/PythonDe...676378bbbda572.

-- Paul

Istvan Albert
Guest
Posts: n/a

 10-04-2006
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.

Wildemar Wildenburger
Guest
Posts: n/a

 10-04-2006
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?

Duncan Booth
Guest
Posts: n/a

 10-05-2006
Wildemar Wildenburger <(E-Mail Removed)> 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.