Observer implementations

Mike C. Fletcher mcfletch at vrplumber.com
Wed Jun 17 14:44:42 EDT 2009


Tobias Weber wrote:
> In article <mailman.1631.1245160392.8015.python-list at python.org>,
>  "Mike C. Fletcher" <mcfletch at vrplumber.com> wrote:
>
>   
>> See PyDispatcher for code to do this.
>>     
>
> That was the original problem. Got it now: if used inside the class 
> definition dispatcher.connect will raise "cannot create weak reference 
> to 'classmethod' object". Outside (using Class.method) it works fine.
>   
Ah, I think you've got a logic problem there.  The classmethod during
class creation has no reference to the class being created, so if you
were to call *that* thing it would blow up:

class x( object ):
    @classmethod
    def y( cls, text ):
        print text
    y( 'No you do not!' )

if you were to create a reference to *that* thing (the decorated
un-bound class method) it would never have the binding for cls, so it
wouldn't do anything.  The correct way IMO to bind after-class-creation
(e.g. by a class decorator or metaclass) where you reference a bound
class method.  If you *really* want to do it this way, you can do
something like this:

class x( object ):
    @classmethod
    def y( cls, value ):
        pass
    dispatcher.connect( lambda *args: x.y( *args ), signal='hello' )

but ick.

HTH,
Mike

-- 
________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com




More information about the Python-list mailing list