addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwchatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgoogleimageimagesinstagramlinklocation-pinmagnifying-glassmailminusmoremuplabelShape 3 + Rectangle 1outlookpersonplusprice-ribbonImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruseryahoo

Re: [pyatl] Good (code | tutorial) on (designing proxies | modifying attribute access)?

From: Doug H.
Sent on: Sunday, December 19, 2010 10:51 AM
On Dec 19, 2010, at 9:26 AM, Leif Poorman wrote:

> Hi, everybody.  If this is an incorrect list to post stuff like this,
> just let me know.  It's about some fundamentals of python, though, so
> I thought it might make a good discussion.
> I was trying to see how well I understood attribute access in python.
> So, I tried to design a class to proxy an object, intercepting gets,
> sets, and dels.  My original goal was to also understand its behavior
> when subclassed.  Simple, right?  Well, let's just say I don't
> understand attributes as well as I thought I did.  Add inheritance,
> and I bet I hardly understand at all.
> I can't find a good tutorial on designing proxy classes, anyone know
> of one?  If not, do you have a favorite Proxy class from a library
> (preferably with a clean, well-commented implementation)?  A lot of
> the implementations of libraries' proxy classes I've looked at have
> strange hacks.   Last resort, can you recommend a good article on
> attribute access?
> Thanks in advance,
> Leif
> P.S.
> #### You can stop reading here, if you want.  What follows is a semi-rant. ####
> I've already asked this on IRC; here are the comments you don't have
> to make, they've been made already:
> # "Why are you proxying?  That is bad, for unspecified reasons!"
> I'd be interested in hearing the specific reasons, and the alternatives.

It's harder to get right than it appears at first blush (as you've discovered).  That doesn't make it wrong, but it may not be the path of least resistance to your goal.  

The weakref module has a proxy class that may be instructive.

> # "Proxying is useless!"
> # Me: "what if I want to instrument unrelated classes A, B, C, for
> profiling/logging/et­c.  Writing a LoggingProxy/etc., seems like the
> best solution to me."
> # "use the profile module"

I'd use the logging or profile modules for those purposes, too.

> # Me: "what if you don't want to profile time spent, but some other
> strange metric that comes up?  Of course I am going to use
> well-established tools for well-established problems, but my question
> was about how to handle instrumentation generally.  Specifically, it
> was about how to design proxies."
> # "don't use proxies, they're hard to write correctly, aren't useful,
> and there are alternatives"
> Again, what are the alternatives?  I guess one alternative might be
> metaclasses, but that doesn't really get around the "hard to write
> correctly" problem, does it?  And what if I don't want to touch
> classes A, B, C, even to add a metaclass?

The trace module may help with generic instrumentation (http://docs.pytho...­ and http://www.doughe...­).

Maybe if you describe the problem you think proxies will solve, we can make a more specific recommendation for an alternative approach (or even confirm that, yes, you need to learn enough about all of the ways attributes are accessed to implement a proper proxy).

> # "read the docs on getattr and setattr"
> Gee, why didn't I think of that?
> --
> Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
> http://www.meetup...­
> This message was sent by Leif Poorman ([address removed]) from PyAtl: Atlanta Python Programmers.
> To learn more about Leif Poorman, visit his/her member profile: http://www.meetup...­
> To unsubscribe or to update your mailing list settings, click here: http://www.meetup...­
> Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy