[TIP] weirdity with identity of Mock

Michael Foord michael at voidspace.org.uk
Wed Jan 25 13:59:21 PST 2012


On 25/01/2012 19:56, Chris Withers wrote:
> So, this won't come as much surprise:
>
> >> class MyClass(object): pass
> ...
> >>> obj1 = MyClass()
> >>> obj2 = MyClass()
> >>> obj1 == obj2
> False
> >>> type(obj1) is type(obj2)
> True
>
> ...but this certainly surprised me:
>
> >>> from mock import Mock
> >>> obj1 = Mock()
> >>> obj2 = Mock()
> >>> type(obj1) is type(obj2)
> False
>
> Wuh?
>
> >>> Mock is Mock
> True
>
> Mmmmkay...
>
> *backs away slightly scared*

Best not to look too closely at how the sausage is made. ;-)

It's part of the magic that allows you to attach a magic method to one 
mock (and magic methods are looked up on the class remember) without it 
affecting other mocks... Look inside __new__ for the gory details.

Michael



>
> Chris
>


-- 
http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html




More information about the testing-in-python mailing list