== versus is operators and type()

Jp Calderone exarkun at intarweb.us
Wed Feb 26 13:42:17 EST 2003


On Wed, Feb 26, 2003 at 02:31:59PM -0300, Alvaro Figueiredo wrote:
> Hi, All.
> 
> When dealing with type() function is the same to use == operator and 
> 'is' operator?
> 
> As an example, are the two expressions bellow totally equivalent?
> 
> type(arg) is type(0) or type(arg) is type(0.0)
> 
> type(arg) in (type(0), type(0.0))
> 

  Yes, short of nasty trickery, such as:

    >>> class Foo(type):
    ...   def __eq__(self, other): return False
    ...
    >>> class Bar(object):
    ...   __metaclass__ = Foo
    ...
    >>> b = Bar()
    >>> type(b) is type(b)
    True
    >>> type(b) == type(b)
    False

  Types are objects like any other, in Python.  Their normal behavior is
sane - an object compares as equal to itself - but this can be overridden.

  Jp

-- 
C/C++/Java/Perl/Python/Smalltalk/PHP/ASP/XML/Linux (User+Admin)
Genetic Algorithms/Genetic Programming/Neural Networks
Networking/Multithreading/Legacy Code Maintenance/OpenGL
See my complete resume at http://intarweb.us:8080/
-- 
 up 17 days, 22:29, 1 user, load average: 0.13, 0.13, 0.15
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030226/33c039c2/attachment.sig>


More information about the Python-list mailing list