Observer implementations

T

Tobias Weber

Hi,
how to use the Observer pattern in Python?

I found PubSub and PyDispatcher, both of which are abandoned. The former
lives on inside wx, the latter as Louie. I have no idea how Louie is
different, but it seems to be abandoned as well. There also is a
PyDispatcher copy in wx.

Despite the confusion all those are useable, but I ran into the problem
that I can't register a @classmethod because weakref doesn't like them.
 
C

Carl Banks

No time to reinvent the wheel

Hmm, observer pattern seems to be one of those things simple enough
that reimplementing the wheel might actually be less work than
adapting a third-party implementation. But I'll leave that decision
to you.

I'd still need to know how to weakref a classmethod

I can't imagine why you would need to weak reference a class method,
since they usually live forever along with the class. But again, you
are doing it so you would know. I can't think of an easy way to do it
without metaclass programming.

Unless you are trying to create a weakref to a bound classmethod. I'd
say that's still pretty useless: bound classmethods only keep alive
classes and classmethods, which both live forever, and bound
classmethods are lightweight and hardly worth the effort.

Maybe show us what you need to weak reference classmethods for and we
can help you better.


Carl Banks
 
M

Mike C. Fletcher

Tobias Weber wrote:
....
No time to reinvent the wheel

I'd still need to know how to weakref a classmethod
See PyDispatcher for code to do this.

PyDispatcher, at least, is not abandoned, it would be more accurate to
say "finished". I use it in OpenGLContext (extensively), but I haven't
had to change anything in a rather long time. It pretty much just works.

Enjoy,
Mike

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

Tobias Weber

"Mike C. Fletcher said:
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.
 
M

Mike C. Fletcher

Tobias said:
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top