Michael Foord
That works fine in Python 3 (mock.Mock does it):
class Foo(object): ... @property ... def __class__(self): ... return int ... a = Foo() isinstance(a, int) True a.__class__
There must be something else going on here.
Michael, thanks for the quick response. Okay, I'll dig in a bit further: the definition in SimpleLazyObject is __class__ = property(new_method_proxy(operator.attrgetter("__class__"))) so perhaps the problem is something related to the specifics of the definition. Here's what I found in initial exploration: -------------------------------------------------------------------------- Python 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information.
from django.utils.functional import SimpleLazyObject fake_bool = SimpleLazyObject(lambda: True) fake_bool.__class__
fake_bool.__dict__ {'_setupfunc': , '_wrapped': True} SimpleLazyObject.__dict__ dict_proxy({ '__module__': 'django.utils.functional', '__nonzero__': , '__deepcopy__': , '__str__': , '_setup': , '__class__': , '__hash__': , '__unicode__': , '__bool__': , '__eq__': , '__doc__': '\n A lazy object initialised from any function.\n\n Designed for compound objects of unknown type. For builtins or objects of\n known type, use django.utils.functional.lazy.\n ', '__init__': })
Python 3.2.2 (default, Sep 5 2011, 21:17:14) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information.
from django.utils.functional import SimpleLazyObject fake_bool = SimpleLazyObject(lambda : True) fake_bool.__class__
fake_bool.__dict__ { '_setupfunc': , '_wrapped':
In Python 3, there's no __class__ property as there is in Python 2, the fake_bool's type isn't bool, and the callable to set up the wrapped object never gets called (which is why _wrapped is not set to True, but to an anonymous object - this is set in SimpleLazyObject.__init__). Puzzling! Regards, Vinay Sajip