By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,399 Members | 886 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,399 IT Pros & Developers. It's quick & easy.

A Universe Set

P: n/a
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 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
jo*********@gmail.com 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
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 4 '06 #2

P: n/a
jo*********@gmail.com 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

Oct 4 '06 #3

P: n/a
On Tue, 03 Oct 2006 23:13:08 -0500, Robert Kern <ro*********@gmail.comwrote:
jo*********@gmail.com 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!
Oct 4 '06 #4

P: n/a
Jorgen Grahn wrote:
- infinite xrange()s
itertools.count()?
Oct 4 '06 #5

P: n/a
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

Oct 4 '06 #6

P: n/a
"Jorgen Grahn" <gr********@snipabacken.dyndns.orgwrote in message
news:sl***********************@frailea.sa.invalid. ..
- 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
Oct 4 '06 #7

P: n/a
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.

Oct 4 '06 #8

P: n/a
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?
Oct 4 '06 #9

P: n/a
Wildemar Wildenburger <wi******@freakmail.dewrote:
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
should aDict[dontcare] do?

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.

Oct 5 '06 #10

P: n/a
On Wed, 04 Oct 2006 11:00:28 -0400, Leif K-Brooks <eu*****@ecritters.bizwrote:
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!
Oct 5 '06 #11

P: n/a
On Thu, 05 Oct 2006 00:02:51 +0200, Wildemar Wildenburger <wi******@freakmail.dewrote:
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!
Oct 5 '06 #12

P: n/a
On 10/4/06, Wildemar Wildenburger <wi******@freakmail.dewrote:
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_p...mathematics%29

-- Theerasak
Oct 6 '06 #13

P: n/a
- 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
Oct 16 '06 #14

P: n/a
"Jorgen Grahn" <gr********@snipabacken.dyndns.orgwrote in message
news:sl***********************@frailea.sa.invalid. ..
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
Oct 16 '06 #15

P: n/a
>- the wildcard object, which compares equal to everything else
Paulclass MatchAny(object):
Paul def __cmp__(self,other):
Paul return 0

Paulwild = 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 '06 #16

P: n/a
On 10/16/06, sk**@pobox.com <sk**@pobox.comwrote:
>
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
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
Oct 16 '06 #17

P: n/a
On Mon, 16 Oct 2006 09:21:11 -0500, sk**@pobox.com <sk**@pobox.comwrote:
>
[me]
>- the wildcard object, which compares equal to everything else
[Paul]
Paulclass MatchAny(object):
Paul def __cmp__(self,other):
Paul return 0

Paulwild = 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!
Oct 17 '06 #18

P: n/a
Jorgen Grahn <gr********@snipabacken.dyndns.orgwrote:
> >- the wildcard object, which compares equal to everything else
[Paul]
> Paulclass MatchAny(object):
Paul def __cmp__(self,other):
Paul return 0

Paulwild = 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.
Oct 17 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.