473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

default object comparison considered harmful?

Hello all,

Yesterday we found the cause of a bug that has caused problems for a long time.
It appeared to be the following:

class A(object):
pass

print min(1.0, A())

which is accepted by Python even though the A() object is not numerical in
nature.

The cause of this behavior seems to be the compare operation of the object
class.
Is there a way to disable this behavior in Python (other than deriving a new
'object-like' class that doesn't do comparisons?)
Sincerely,
Albert
Jun 27 '08 #1
11 1231
A.T.Hofkamp a écrit :
Hello all,

Yesterday we found the cause of a bug that has caused problems for a long time.
It appeared to be the following:

class A(object):
pass

print min(1.0, A())

which is accepted by Python even though the A() object is not numerical in
nature.

The cause of this behavior seems to be the compare operation of the object
class.
Is there a way to disable this behavior in Python (other than deriving a new
'object-like' class that doesn't do comparisons?)
Other than implementing a custom __cmp__ function ? Not AFAIK. FWIW, you
don't necessarily need to fiddle with inheritance - you can also
monkeypatch your classes (nb : for new-style objects, you cannot
monkeypatch __magic__ methods on a per-object basis).

Also, IIRC, this behaviour has changed in Python 3.

Jun 27 '08 #2
A.T.Hofkamp wrote:
Yesterday we found the cause of a bug that has caused problems for a long
time. It appeared to be the following:

class A(object):
pass

print min(1.0, A())

which is accepted by Python even though the A() object is not numerical in
nature.

The cause of this behavior seems to be the compare operation of the object
class.
Is there a way to disable this behavior in Python (other than deriving a
new 'object-like' class that doesn't do comparisons?)
As Bruno says, this will change in 3.0:

# 3.0
>>class A: pass
....
>>min(1.0, A())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: A() < float()

For 2.5 and above you can provide a key function as a workaround:

# 2.5
>>class A(object): pass
....
>>min(1, A(), key=float)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: float() argument must be a string or a number

Peter
Jun 27 '08 #3
On 16 Mai, 10:03, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
Hello all,

Yesterday we found the cause of a bug that has caused problems for a long time.
It appeared to be the following:

class A(object):
pass

print min(1.0, A())

which is accepted by Python even though the A() object is not numerical in
nature.

The cause of this behavior seems to be the compare operation of the object
class.

Is there a way to disable this behavior in Python (other than deriving a new
'object-like' class that doesn't do comparisons?)

Sincerely,
Albert
Are you sure you don't want to use a statically typed language that
captures all type errors just by inspecting your source code?
Jun 27 '08 #4
Kay Schluehr a écrit :
On 16 Mai, 10:03, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
>Hello all,

Yesterday we found the cause of a bug that has caused problems for a long time.
It appeared to be the following:

class A(object):
pass

print min(1.0, A())

which is accepted by Python even though the A() object is not numerical in
nature.

The cause of this behavior seems to be the compare operation of the object
class.

Is there a way to disable this behavior in Python (other than deriving a new
'object-like' class that doesn't do comparisons?)
Are you sure you don't want to use a statically typed language that
captures all type errors just by inspecting your source code?
This is not necessarily a static vs dynamic typing problem. The
following code will raise a TypeError for very good reasons:

print 1.0 + A()
Jun 27 '08 #5
On May 16, 7:23*am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
Kay Schluehr a écrit :


On 16 Mai, 10:03, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
Hello all,
Yesterday we found the cause of a bug that has caused problems for a long time.
It appeared to be the following:
class A(object):
* * pass
print min(1.0, A())
which is accepted by Python even though the A() object is not numericalin
nature.
The cause of this behavior seems to be the compare operation of the object
class.
Is there a way to disable this behavior in Python (other than deriving a new
'object-like' class that doesn't do comparisons?)
Are you sure you don't want to use a statically typed language that
captures all type errors just by inspecting your source code?

This is not necessarily a static vs dynamic typing problem. The
following code will raise a TypeError for very good reasons:

* * print 1.0 + A()- Hide quoted text -

- Show quoted text -
I question the real-world examples of coercion, casting, and
upcasting. C insists that defining by operator, which may or may not
relate to system, is the only legal move. I doubt operator+ is well-
defined on non-ideal structures. Rationals are not floats, but system
got checked. Define concurrency.
Jun 27 '08 #6
On May 16, 8:18*am, castironpi <castiro...@gmail.comwrote:
On May 16, 7:23*am, Bruno Desthuilliers <bruno.

42.desthuilli...@websiteburo.invalidwrote:
Kay Schluehr a écrit :
On 16 Mai, 10:03, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
>Hello all,
>Yesterday we found the cause of a bug that has caused problems for a long time.
>It appeared to be the following:
>class A(object):
>* * pass
>print min(1.0, A())
>which is accepted by Python even though the A() object is not numerical in
>nature.
>The cause of this behavior seems to be the compare operation of the object
>class.
>Is there a way to disable this behavior in Python (other than deriving a new
>'object-like' class that doesn't do comparisons?)
Are you sure you don't want to use a statically typed language that
captures all type errors just by inspecting your source code?
This is not necessarily a static vs dynamic typing problem. The
following code will raise a TypeError for very good reasons:
* * print 1.0 + A()- Hide quoted text -
- Show quoted text -

I question the real-world examples of coercion, casting, and
upcasting. *C insists that defining by operator, which may or may not
relate to system, is the only legal move. *I doubt operator+ is well-
defined on non-ideal structures. *Rationals are not floats, but system
got checked. *Define concurrency.- Hide quoted text -

- Show quoted text -
The computer will have to learn knowledge at a distributed real-time
rate.
Jun 27 '08 #7
On May 16, 8:18*am, castironpi <castiro...@gmail.comwrote:
On May 16, 7:23*am, Bruno Desthuilliers <bruno.

42.desthuilli...@websiteburo.invalidwrote:
Kay Schluehr a écrit :
On 16 Mai, 10:03, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
>Hello all,
>Yesterday we found the cause of a bug that has caused problems for a long time.
>It appeared to be the following:
>class A(object):
>* * pass
>print min(1.0, A())
>which is accepted by Python even though the A() object is not numerical in
>nature.
>The cause of this behavior seems to be the compare operation of the object
>class.
>Is there a way to disable this behavior in Python (other than deriving a new
>'object-like' class that doesn't do comparisons?)
Are you sure you don't want to use a statically typed language that
captures all type errors just by inspecting your source code?
This is not necessarily a static vs dynamic typing problem. The
following code will raise a TypeError for very good reasons:
* * print 1.0 + A()- Hide quoted text -
- Show quoted text -

I question the real-world examples of coercion, casting, and
upcasting. *C insists that defining by operator, which may or may not
relate to system, is the only legal move. *I doubt operator+ is well-
defined on non-ideal structures. *Rationals are not floats, but system
got checked. *Define concurrency.- Hide quoted text -

- Show quoted text -
I would be bouncing localcy and remoticy. What word's on the bar?
Jun 27 '08 #8
On May 16, 8:35*am, castironpi <castiro...@gmail.comwrote:
On May 16, 8:18*am, castironpi <castiro...@gmail.comwrote:


On May 16, 7:23*am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
Kay Schluehr a écrit :
On 16 Mai, 10:03, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
Hello all,
Yesterday we found the cause of a bug that has caused problems for a long time.
It appeared to be the following:
class A(object):
* * pass
print min(1.0, A())
which is accepted by Python even though the A() object is not numerical in
nature.
The cause of this behavior seems to be the compare operation of theobject
class.
Is there a way to disable this behavior in Python (other than deriving a new
'object-like' class that doesn't do comparisons?)
Are you sure you don't want to use a statically typed language that
captures all type errors just by inspecting your source code?
This is not necessarily a static vs dynamic typing problem. The
following code will raise a TypeError for very good reasons:
* * print 1.0 + A()- Hide quoted text -
- Show quoted text -
I question the real-world examples of coercion, casting, and
upcasting. *C insists that defining by operator, which may or may not
relate to system, is the only legal move. *I doubt operator+ is well-
defined on non-ideal structures. *Rationals are not floats, but system
got checked. *Define concurrency.- Hide quoted text -
- Show quoted text -

I would be bouncing localcy and remoticy. *What word's on the bar?- Hidequoted text -

- Show quoted text -
Can you suffixize words? I have corner, cornercy, lunacy, emphatic,
emphasize, a value of Y, orderings, say, remoticy, & cross. Come do
graphics. Anybody speak?

For verbs, I have: say, speak, see, talk, and stack. Pretty
harmless. Still here?
Jun 27 '08 #9
On May 16, 9:10*am, castironpi <castiro...@gmail.comwrote:
On May 16, 8:35*am, castironpi <castiro...@gmail.comwrote:


On May 16, 8:18*am, castironpi <castiro...@gmail.comwrote:
On May 16, 7:23*am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
Kay Schluehr a écrit :
On 16 Mai, 10:03, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
>Hello all,
>Yesterday we found the cause of a bug that has caused problems for a long time.
>It appeared to be the following:
>class A(object):
>* * pass
>print min(1.0, A())
>which is accepted by Python even though the A() object is not numerical in
>nature.
>The cause of this behavior seems to be the compare operation of the object
>class.
>Is there a way to disable this behavior in Python (other than deriving a new
>'object-like' class that doesn't do comparisons?)
Are you sure you don't want to use a statically typed language that
captures all type errors just by inspecting your source code?
This is not necessarily a static vs dynamic typing problem. The
following code will raise a TypeError for very good reasons:
* * print 1.0 + A()- Hide quoted text -
- Show quoted text -
I question the real-world examples of coercion, casting, and
upcasting. *C insists that defining by operator, which may or may not
relate to system, is the only legal move. *I doubt operator+ is well-
defined on non-ideal structures. *Rationals are not floats, but system
got checked. *Define concurrency.- Hide quoted text -
- Show quoted text -
I would be bouncing localcy and remoticy. *What word's on the bar?- Hide quoted text -
- Show quoted text -

Can you suffixize words? *I have corner, cornercy, lunacy, emphatic,
emphasize, a value of Y, orderings, say, remoticy, & cross. *Come do
graphics. *Anybody speak?

For verbs, I have: say, speak, see, talk, and stack. *Pretty
harmless. *Still here?- Hide quoted text -

- Show quoted text -
That's stay tuned I'M BARTENDER. Is that a space BAR ZOOP.

Jun 27 '08 #10
| On 16 Mai, 10:03, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
| Yesterday we found the cause of a bug that has caused problems for a
long time.
| It appeared to be the following:
| >
| class A(object):
| pass
| >
| print min(1.0, A())
| >
| which is accepted by Python even though the A() object is not numerical
in
| nature.
| >
| The cause of this behavior seems to be the compare operation of the
object
| class.
| >
| Is there a way to disable this behavior in Python (other than deriving
a new
| 'object-like' class that doesn't do comparisons?)

Use Python 3! That is one of many improvements.

Jun 27 '08 #11
On 2008-05-16, Kay Schluehr <ka**********@gmx.netwrote:
On 16 Mai, 10:03, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
>Hello all,

Yesterday we found the cause of a bug that has caused problems for a long time.
It appeared to be the following:

class A(object):
pass

print min(1.0, A())

which is accepted by Python even though the A() object is not numerical in
nature.

The cause of this behavior seems to be the compare operation of the object
class.

Is there a way to disable this behavior in Python (other than deriving a new
'object-like' class that doesn't do comparisons?)

Sincerely,
Albert

Are you sure you don't want to use a statically typed language that
captures all type errors just by inspecting your source code?
yes.

The problem here is that it isn't caught at all, neither at 'compile' time nor
at run-time. That means that the Python language considers this proper code.

Whether you make that decision by inspecting source code or at run-time is
irrelevant here.
Unfortunaly, we have to maintain Python 2.3 compability.
As a solution, I have created a new BaseObject class as follows:

class BaseObject(object):
"""
Generic base class without default compare and hashing functions.
"""
def __cmp__(self, other):
""" Disable default compare method """
raise NotImplementedError

def __hash__(self):
""" Disable default hashing method """
raise NotImplementedError("Implement me in class '%s'"
% self.__class__.__name__)
Sincerely,
Albert
Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: mike420 | last post by:
map(lambda f: f(1), ) Oh, OK, it was a typo (1 instead of i). I take it all back (for now). It was an honest mistake, not a troll! Still, I think it should be instead of
0
by: SQLServer007 | last post by:
Download a FREE fully functional trial from http://www.x-sql.com NEW FOR xSQL OBJECT VERSION 1.5: - Support for full-text indexes and full-text catalogs; - Support for scripting of User's...
5
by: Mike Nolan | last post by:
I notice that 7.4 doesn't do default ordering on a 'group by', so you have to throw in an 'order by' clause to get the output in ascending group order. Is this something that most RDB's have...
37
by: Michele Simionato | last post by:
At work we are shopping for a Web framework, so I have been looking at the available options on the current market. In particular I have looked at Paste and Pylons and I have written my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.