tags:

views:

46

answers:

2

I'm writing a class which encapsulates any arbitrary object, including simple types. I want the "is" keyword to operate on the encapsulated value, such as this behavior:

Wrapper(True) is True -> True
Wrapper(False) is True -> False
Wrapper(None) is None -> True
Wrapper(1) is 1 -> True

Is there any object method I can override to get this behavior?

+5  A: 

No. is, and, and or cannot be overloaded.

Ignacio Vazquez-Abrams
Is that in Python documentation anywhere? Can't find anything verifying this.
Heinrich Schmetterling
The only reference is their absence in §3.4 of the langref. http://docs.python.org/reference/datamodel.html#special-method-names
Ignacio Vazquez-Abrams
+1  A: 

Generally, if you want to test equality in terms of value (if x is 1, or True, or None), you'd use the == operator anyway. If you want to use the is operator, you're generally testing if something is referring to something else, like list1 is list2.

If you want to define special behavior for ==, you can define __eq__ in your class definition.

Rafe Kettler