is decorator the right thing to use?

Dmitry S. Makovey dmitry at makovey.net
Sat Sep 27 01:44:24 EDT 2008


George Sakkis wrote:
> You seem to enjoy pulling the rug from under our feet by changing the
> requirements all the time :)

but that's half the fun! ;)

Bit more seriously - I didn't know I had those requirements until now :) I'm
kind of exploring where can I get with those ideas. Initial post was based
exactly on what I had in my code with desire to make it more
automated/{typo,fool}-proof/robust, elegant and possibly faster. Hopefully
such behavior is not off-topic on this list (if it is - let me know and
I'll go exploring solo).

> Although this works, the second argument to ProxyMethod shouldn't be
> necessary, it's semantically redundant;  ideally you would like to 
> write it as "bmethod = ProxyMethod('b')".

since I'm already on exploratory trail (what about that rug being pulled
from under....?) With my code I can do really dirty tricks like this (not
necessary that I'm going to):

class B_base:
        def bmethod(self):
                print 'B_base'

class B(B_base):
        def bmethod(self):
                print 'B'

class A:
        bmethod=ProxyMethod('b',B_base.bmethod)

> As before, I don't think
> that's doable without metaclasses (or worse, stack frame hacking).
> Below is the update of my original recipe; interestingly, it's
> (slightly) simpler than before:

Out of curiosity (and trying to understand): why do you insist on
dictionaries with strings contents ( {'bmethod' : 'b1' } ) rather than
something more explicit ? Again, I can see that your code is working and I
can even understand what it's doing, just trying to explore alternatives :) 

I guess my bias is towards more explicit declarations thus 

 bmethod=ProxyMethod('b',B.bmethod) 

looks more attractive to me, but I stand to be corrected/educated why is
that not the right thing to do?

Another thing that turns me away from string dictionaries is that those are
the ones causing me more trouble hunting down typos. Maybe it's just "my
thing" so I'm not going to insist on it. I'm open to arguments against that
theory.

One argument I can bring in defence of more explicit declarations is IDE
parsing when autocompletion for B.bme... pops up (suggesting bmethod and
bmethod2) and with 'b':'bmethod' it never happens. However that's not good
enough reason to stick to it if it introduces other problems. What kind of
problems could those be?

P.S.
so far I find this discussion quite educating BTW. I am more of a "weekend
programmer" with Python - using basic language for most of things I need,
however I decided to explore further to educate myself and to write more
efficient code.





More information about the Python-list mailing list