Checking for usual descriptors in Python 2.3

  • Thread starter =?iso-8859-1?Q?Fran=E7ois?= Pinard
  • Start date
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

This question is a bit technical, but hopefully, this list will offer me
good hints or nice solutions. Happily enough for me, it often does! :)

I would need to recognise and play with descriptor types, like:

member descriptors
method descriptors
getset descriptors
wrapper descriptors

but do not find how to easily refer to them, either from existing
constructor types (like we could do with `property', say), nor from
members of the `types' module. I also wonder how much I can "get into"
these various descriptors or tear them apart...


P.S. - If you are curious, my real goal is creating some metaclass able
to build kind-of-shadow classes for various pygtk widgets, lazily, on
the fly. If experimentation shows that this is not speedy enough, than
I may rather have a pre-processor that would produce Python source
instead. In any case, I need to learn how to explore descriptors at run
time, rather than working hard at scanning/parsing real pygtk sources.
 
M

Michael Hudson

François Pinard said:
This question is a bit technical, but hopefully, this list will offer me
good hints or nice solutions. Happily enough for me, it often does! :)

I would need to recognise and play with descriptor types, like:

member descriptors
method descriptors
getset descriptors
wrapper descriptors

but do not find how to easily refer to them, either from existing
constructor types (like we could do with `property', say), nor from
members of the `types' module. I also wonder how much I can "get into"
these various descriptors or tear them apart...

If you want to know whether 'ob' is a descriptor,

hasattr(ob, '__get__')

is pretty close. PEP 252 is also your friend.
P.S. - If you are curious, my real goal is creating some metaclass
able to build kind-of-shadow classes for various pygtk widgets,
lazily, on the fly.

I'm not sure how this reletes to the above, but have fun :)

You may find it sensible to just restrict yourself to a finite set of
descriptors you know how to deal with.

Cheers,
mwh
 
M

Michael Hudson

Michael Hudson said:
If you want to know whether 'ob' is a descriptor,

hasattr(ob, '__get__')

is pretty close. PEP 252 is also your friend.

On the off chance that this wasn't what you were asking :)-) and you
wanted to get your hands on the type objects of each of the given
descriptor types, the only way I can think of doing that is getting
your hands on one and calling type() on it.

member: type(type.__dict__['__dictoffset__'])
method: type(list.append)
getset: type(object.__dict__['__class__'])
wrapper: type(object.__dict__['__getattribute__'])

As to how introspectable each of these are, the interactive
interpreter (or the source) is now your friend.

HTH,
mwh
 

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

Forum statistics

Threads
474,178
Messages
2,570,955
Members
47,509
Latest member
Jack116

Latest Threads

Top