Josiah Carlson ([info]chouyu_31) wrote,
@ 2003-12-22 00:32:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
A Case for Top and Bottom Values
EDIT:
I've removed the text for the original submission. Why? Because it was ugly as hell. No reason to keep it.

I will instead now link to the official copy of PEP 326.

On the occasion where I update it before it gets into CVS (as is wont to happen), I'll be placing a local copy on my machine, which would be available here. Please note, my machine gets turned off when I go to sleep.



(Post a new comment)


[info]valrus
2003-12-22 06:31 am UTC (link)
Awesome idea. I remember the algorithms book at Macalester always using $\infty$ to represent a "larger than anything else" value and thinking about how much prettier the actual code would be if it were possible to do the same thing, which it invariably was not.

I'm not sure about the name "Some," as to me that doesn't really imply "larger than everything else," but on the other hand it does seem like the best counterpart to "None."

Cool, though. I hope it gets implemented; if not, I can use the implementation you so kindly provided here.

(Reply to this) (Thread)


[info]chouyu_31
2003-12-22 11:27 am UTC (link)
Yeah, I think it is a great idea. Unfortunately Guido, Tim, and a few others are against the idea. If you want to, check out the emails entitled "Got None. Maybe Some?" in the Python-dev mailing list archives here.

On the upside, someone does offer a different implementation, but it turns out to not be any faster, just more explicit about comparisons (and it tosses out the __hash__ method that is unneeded).

Ah well, I'll probably use a combination of mine and the other:
class _SomeType(object):
    def __cmp__(self, other):
        if other is Some:
            return 0
        return 1
    def __repr__(self):
        return "Some"
Some = _SomeType()

(Reply to this) (Parent)(Thread)

It's not really Some is it?
[info]etrepum
2004-01-06 11:27 am UTC (link)
I think Some is a terrible name, and I think using None is more of a hack than a solution because it's not obvious that None is smaller than EVERY object. This is what I have used:
class _MaxObject:
    def __cmp__(self, other):
        return int(other is not self)

    def __eq__(self, other):
        return other is self

    def __neq__(self, other):
        return other is not self

    def __hash__(self):
        return id(self)

class _MinObject:
    def __cmp__(self, other):   
        return -int(other is not self)
    
    def __eq__(self, other):    
        return other is self     

    def __neq__(self, other):
        return other is not self

    def __hash__(self):
        return id(self)

maxobject = _MaxObject()
minobject = _MinObject()

(Reply to this) (Parent)(Thread)

Re: It's not really Some is it?
[info]chouyu_31
2004-01-06 12:44 pm UTC (link)
The name has been changed significantly since this original post.

I would suggest that you read the PEP.

(Reply to this) (Parent)(Thread)

Re: It's not really Some is it?
[info]etrepum
2004-01-06 01:35 pm UTC (link)
...
While None can be used as an absolute minimum that any value can attain [1], this may be depreciated [5] in Python 3.0, and shouldn't be relied upon.
...

that should be deprecated :)

An alternative idea would be to use max and min as cmp.high and cmp.low, that would be pretty cool and intuitive I think. They would of course, be callable objects.

(Reply to this) (Parent)(Thread)

Re: It's not really Some is it?
[info]chouyu_31
2004-01-06 02:01 pm UTC (link)
That is an interesting idea. The only thing is that they would be min/max functions primarily, that happen to have the behavior of comparing lower or higher (respectively) than any other object.

I do like it though.

(Reply to this) (Parent)(Thread)

Re: It's not really Some is it?
[info]etrepum
2004-01-06 02:26 pm UTC (link)
type is a "function" (read: callable object) that typcially returns the type of an object, but it creates a subtype if you pass it three arguments. type is also a type.

I don't see how making max and min more useful could possibly be any "worse" than that. I think it's really cool actually. max is a singleton that serves as a maximum value for any comparison that happens to have a callable that returns the maximum object in a sequence (read: something that supports the iterator protocol implicitly or explicitly).

I really think it's more attractive than adding members to cmp, which is a worse offense in my book. I don't know anywhere in (typical standard) Python where foo.bar is some useful type or singleton, except when foo is a module or package.

(Reply to this) (Parent)(Thread)

Re: It's not really Some is it?
[info]chouyu_31
2004-01-06 06:59 pm UTC (link)
You don't need to convince me, I'm sold on the min being the minimum and max being the maximum. I've already emailed the mailing list and offered it as an option, and am going to put it in the PEP as an "Open Issue".

(Reply to this) (Parent)


Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…