Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the `is` operator [duplicate]

Possible Duplicate:
python: class override “is” behavior

I am trying to override the is operator, so that I could do something like

if tom is writer:
   print 'tom is writing'
elif tom is programmer:
   print 'tom is programming'

Is this possible in python?

like image 257
satoru Avatar asked Sep 06 '25 03:09

satoru


1 Answers

As far as I know, you can't override 'is' because it tests whether two objects are the same object by comparing their memory addresses (i.e. pointer comparison). You probably want to override '==' by implementing tom's __eq__ method.

like image 124
codewarrior Avatar answered Sep 07 '25 16:09

codewarrior