472,986 Members | 2,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

Checking for usual descriptors in Python 2.3

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.

--
François Pinard http://www.iro.umontreal.ca/~pinard

Jul 18 '05 #1
2 1778
François Pinard <pi****@iro.umontreal.ca> writes:
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

--
I'm a keen cyclist and I stop at red lights. Those who don't need
hitting with a great big slapping machine.
-- Colin Davidson, cam.misc
Jul 18 '05 #2
Michael Hudson <mw*@python.net> writes:
François Pinard <pi****@iro.umontreal.ca> writes:
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.


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

--
If you don't use emacs, you're a pathetic, mewling, masochistic
weakling and I can't be bothered to convert you. -- Ron Echeverri
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Denis S. Otkidach | last post by:
I've noticed that the order of attribute lookup is inconsistent when descriptor is used. property instance takes precedence of instance attributes: >>> class A(object): .... def...
6
by: Jeff Duffy | last post by:
Hi all. I've been wondering why python itself doesn't provide a switch to check a file for valid syntax. I know that you can currently call python -c "import py_compile;...
14
by: Antoon Pardon | last post by:
Can anyone explain why descriptors only work when they are an attribute to an object or class. I think a lot of interesting things one can do with descriptors would be just as interesting if the...
8
by: David S. | last post by:
I am looking for a way to implement the same simple validation on many instance attributes and I thought descriptors (http://users.rcn.com/python/download/Descriptor.htm) looked like the right...
0
by: Steven Bethard | last post by:
Steven Bethard wrote: > (For anyone else out there reading who doesn't already know this, > Steven D'Aprano's comments are easily explained by noting that the > __get__ method of staticmethod...
12
by: bruno at modulix | last post by:
Hi I'm currently playing with some (possibly weird...) code, and I'd have a use for per-instance descriptors, ie (dummy code): class DummyDescriptor(object): def __get__(self, obj,...
3
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder...
7
by: mrkafk | last post by:
Hello everyone, I'm trying to do seemingly trivial thing with descriptors: have another attribute updated on dot access in object defined using descriptors. For example, let's take a simple...
1
by: Chris Leary | last post by:
As I understand it, the appeal of properties (and descriptors in general) in new-style classes is that they provide a way to "intercept" direct attribute accesses. This lets us write more clear...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.