Why query != same_query?

40 views
Skip to first unread message

Alan Etkin

unread,
Jan 28, 2013, 5:08:05 PM1/28/13
to web...@googlegroups.com
I have noticed this behavior in trunk. Is this a feature?

>>> q = db.auth_user.id > 0
>>> q2 = db.auth_user.id > 0
>>> q == q2
False

I hope I didn't post a read the manual question again, but doesn't seem to be documented in the DAL section.

Thanks

Alan Etkin

unread,
Jan 28, 2013, 5:21:45 PM1/28/13
to web...@googlegroups.com
I have noticed this behavior in trunk. Is this a feature?

I belive I found the problem: the Query class doesn't implement __eq__ nor __ne__. Is that the cause?

Anthony

unread,
Jan 28, 2013, 5:39:07 PM1/28/13
to web...@googlegroups.com
According to the Python documentation:

If no __cmp__()__eq__() or __ne__() operation is defined, class instances are compared by object identity (“address”).

The Query class doesn't define any of those operations, so object identity will be used, hence the False value (i.e., each query is a separate object with a separate identity). Instead, you can do:

repr(q) == repr(q2)

Anthony

Anthony

unread,
Jan 28, 2013, 5:41:05 PM1/28/13
to web...@googlegroups.com

repr(q) == repr(q2)


I suppose we could implement __eq__() and __ne__() methods that do the above.
 
Anthony

Alan Etkin

unread,
Jan 28, 2013, 5:44:50 PM1/28/13
to web...@googlegroups.com
repr(q) == repr(q2)

+1 for the repr comparison, as long as it's backwards compatible and it doesn't have conflicts with other query operations

Massimo Di Pierro

unread,
Jan 28, 2013, 9:49:32 PM1/28/13
to web...@googlegroups.com
Good idea Anthony, can you send me a patch?

Alan Etkin

unread,
Jan 30, 2013, 6:18:33 AM1/30/13
to web...@googlegroups.com
Good idea Anthony, can you send me a patch?

I added Anthony's patch to issue 1310. Is it ok?

Reply all
Reply to author
Forward
0 new messages