Use of descriptor __get__: what defines an object as being a class?

  • Thread starter John Perks and Sarah Mount
  • Start date
J

John Perks and Sarah Mount

I'm talk from the point of view of descriptors. Consider

a.x = lambda self:None # simple function

When a.x is later got, what criterion is used to see if a class (and so the
func would have __get__(None, a) called on it)? Pre-metaclasses, one might
assume it was

isinstance(a, (types.TypeType, types.ClassType))

but nowadays the type of a type could be anything. Is there a simple
Python-level interface to determine what constitutes a type, as there is
for, say, iterators? Could an object start being a type just by
appropriately setting certain members, or is this distinction enforced at a
lower level?

To confuse matters further, the descriptor docs in 2.4 say that descriptors
can only be bound to objects, not classic class instances, but a few moments
with a Python console disproves this (it is the descriptors themselves that
have to be objects.)
Getting!

This seems to be a more sensible state of affairs (otherwise
function-to-method wrapping would have to be a special case), but
http://www.python.org/doc/2.4/ref/descriptor-invocation.html says different.

What is the canonical distinction between classes and types, and between
instances and objects? How would it apply in the face of such perversions as

from types import *
class C1: __metaclass__ = TypeType
class C2(object): __metaclass__ = ClassType

or reassignment of __class__ from an class to a type or vice versa? I would
have thought that "most of the time" (i.e. excluding the above tricksiness)
all that would be needed was

isinstance(x, object)

but this returns true when x is an Exception, which is guaranteed to be an
instance and hence not an object.

Thanks

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top