Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 23rd, 2006, 8:16 AM   #11
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
(Gnaaah... take two)
Quote:
Originally Posted by DaWei
It wasn't directed at you, Arevos, it was directed at the rewriting of my code...
I apologise for the overbrevity of my comment. As Arevos has suggested, when you compare None (and to a lesser extent True and False) by value (==, !=) rather than by identity (is, is not) you can end up with unexpected results if the object you're comparing is buggy, etc. It's not a common problem, but when it happens it can be painful to track down, so it's better to have the habit of always using identity comparison rather than value comparison even if the object (in this case the regex matches) is (almost) certain to be bugfree.

The following should illustrate:
class Expected:
    pass

class Evil1:
    def __eq__(self, other):
        return self == other

class Evil2:
    def __cmp__(self, other):
        if other is self:
            return True
        return False

for cls in (Expected, Evil1, Evil2):
    x = cls()
    print cls.__name__
    print "   ", x == None, x != None, x is None, x is not None, "\n"
-T.
"One man's cargo cult is another man's defensive."
hydroxide is offline   Reply With Quote
Old May 23rd, 2006, 8:51 AM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Perhaps I oversimplify things. Rather than create examples, I reasoned thusly: Suppose I have two things that, compared as objects, yield one result. When compared by some attribute, they yield the opposite result. One of the two comparisons is "wrong." Which one is wrong depends on the requirements of the writer. The important thing, to me, is to be aware that there's a difference. I WAS NOT AWARE until the comment was made. I thank you for that and for your additional explanation. I will, I must admit, not ascribe evil or safety to one over the other, except as individual circumstance dictates.

I appreciate the help from both of you.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 23rd, 2006, 11:06 AM   #13
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

Assuming the code is correct, my measure of acceptable Python brevity is:

1) Let one of your peers look at it, and see if he/she can figure out how it works (in less than a day).

2) Imagine yourself looking at it after a few months and still know what it does.

Lastly, you can play innocent, put the code on the forum, and ask how it works. If nobody answers, you exceeded the brevity limit!

DaWei, may you be smitten with Python desire real good!
__________________
I looked it up on the Intergnats!

Last edited by Dietrich; May 23rd, 2006 at 11:16 AM.
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC