[Python-Dev] Python 3, new-style classes and class (original) (raw)
Vinay Sajip vinay_sajip at yahoo.co.uk
Sun Nov 20 00:11:38 CET 2011
- Previous message: [Python-Dev] Python 3, new-style classes and __class__
- Next message: [Python-Dev] Python 3, new-style classes and __class__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Michael Foord <fuzzyman voidspace.org.uk> writes:
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 <class 'int'> 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 >>> fakebool = SimpleLazyObject(lambda: True) >>> fakebool.class <type 'bool'> >>> fakebool.dict {'_setupfunc': <function at 0xca9ed8>, '_wrapped': True} >>> SimpleLazyObject.dict dict_proxy({ 'module': 'django.utils.functional', 'nonzero': <function inner at 0xca9de8>, 'deepcopy': <function __deepcopy__ at 0xca9c08>, 'str': <function inner at 0xca9b18>, '_setup': <function _setup at 0xca9aa0>, 'class': <property object at 0xca5730>, 'hash': <function inner at 0xca9d70>, 'unicode': <function inner at 0xca9b90>, 'bool': <function inner at 0xca9de8>, 'eq': <function inner at 0xca9cf8>, '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': <function __init__ at 0xca9a28> })
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 >>> fakebool = SimpleLazyObject(lambda : True) >>> fakebool.class <class 'django.utils.functional.SimpleLazyObject'> >>> fakebool.dict { '_setupfunc': <function at 0x1c36ea8>, '_wrapped': <object object at 0x1d88b70> } >>> SimpleLazyObject.dict dict_proxy({ 'module': 'django.utils.functional', 'nonzero': <function inner at 0x1f56490>, 'deepcopy': <function __deepcopy__ at 0x1f562f8>, 'str': <function inner at 0x1f561e8>, '_setup': <function _setup at 0x1f56160>, 'hash': <function inner at 0x1f56408>, 'unicode': <function inner at 0x1f56270>, 'bool': <function inner at 0x1f56490>, 'eq': <function inner at 0x1f56380>, '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': <function __init__ at 0x1f560d8> })
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
- Previous message: [Python-Dev] Python 3, new-style classes and __class__
- Next message: [Python-Dev] Python 3, new-style classes and __class__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]