[Python-Dev] Python 3, new-style classes and class (original) (raw)

Michael Foord fuzzyman at voidspace.org.uk
Mon Nov 21 18:22:48 CET 2011


On 20/11/2011 21:41, Guido van Rossum wrote:

On Sun, Nov 20, 2011 at 10:44 AM, Michael Foord <fuzzyman at voidspace.org.uk> wrote:

On 20 Nov 2011, at 16:35, Guido van Rossum wrote:

Um, what?! class already has a special meaning. Those examples violate that meaning. No wonder they get garbage results.

The correct way to override isinstance is explained here: http://www.python.org/dev/peps/pep-3119/#overloading-isinstance-and-issubclass .

Proxy classes have been using class as a descriptor for this purpose for years before ABCs were introduced. This worked fine up until Python 3 where the compiler magic broke it when super is used. That is now fixed anyway. Hm, okay. Though it's disheartening that it took three releases of 3.x to figure this out. And there was a PEP even! If I understand correctly, ABCs are great for allowing classes of objects to pass isinstance checks (etc) - what proxy, lazy and mock objects need is to be able to allow individual instances to pass different isinstance checks. Ah, oops. Yes, instancecheck is for the class to override isinstance(inst, cls); for the instance to override apparently you'll need to mess with class. I guess my request at this point would be to replace '@class' with some other legal identifier that doesn't clash with existing use -- I don't like the arbitrary use of @ here.

The problem with using a valid identifier name is that it leaves open the possibility of the same "broken" behaviour (removing from the class namespace) for whatever name we pick.

That means we should document the name used - and it's then more likely that users will start to rely on this odd (but documented) internal implementation detail. This in turn puts a burden on other implementations to use the same mechanism, even if this is less than ideal for them.

This is why a deliberately invalid identifier was picked.

All the best,

Michael Foord

--Guido

All the best,

Michael Foord

--Guido

On Sat, Nov 19, 2011 at 6:13 PM, Michael Foord <fuzzyman at voidspace.org.uk> wrote:

On 19 November 2011 23:11, Vinay Sajip<vinaysajip at yahoo.co.uk> wrote: 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(newmethodproxy(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 dictproxy({ '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 dictproxy({ '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 fakebool'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). The Python compiler can do strange things with assignment to class in the presence of super. This issue has now been fixed, but it may be what is biting you: http://bugs.python.org/issue12370 If this is the problem, then see the workaround suggested in the issue. (alias super to super in the module scope and use the old style super calling convention.) Michael

Puzzling! Regards, Vinay Sajip


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk

-- 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


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org

-- --Guido van Rossum (python.org/~guido) -- 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

-- 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 Python-Dev mailing list