-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fundamental changes for v0.3 #42
Comments
I have a few comments on this. I tried contacting people through the mailing list but got no response, so I guess it's defunct.
Here is my implementation of the observer pattern. You can see how I handle weak reference management in a nice way https://github.com/DanielSank/observed |
First off: sorry for not answering your post in the mailing list. I About your comments:
import weakref
# in lack of a better name:
class WeakObserver(object):
def __init__(self, observer):
self._wr = weakref.ref(observer)
def __call__(self, *args, **kwargs):
obj = self._wr()
if obj is not None:
return obj(*args, **kwargs)
x.add_observer(WeakObserver(obs))
# or, if providing a special function `add_weak_observer` one could
# handle the `ref is None` case as well to automatically disconnect
# the invalid reference.
x.add_weak_observer(obs) I haven't had much thought about this so far, but it seems to me that you are right insofar that this might be a common need and worth adding to the module, possibly with an extra 3. The 4. I don't know whether the extra switch is really necessary, when in the few cases where the self-argument is needed, you can just connect a lambda that adds the extra argument. On the other hand, this will prevent automatic reference counting and make it necessary to delete the observed object with the garbage collector (depending on your python implementation that might be true either way). But it's worth thinking about the extra switch, if it doesn't hurt the tidiness of the code. About your repository: I didn't look at it in detail so far. There are some minor things that I have noticed:
|
Thanks for the reply.
Thanks for the feedback on
If you have a few minutes, I would deeply appreciate it if you could explain why this is. It's probably more appropriate to post as an issue in my repo though, as we are supposed to be talking about |
From a user's perspective, I find a wrapper less confusing than weak-refing by default, this is subjective though.
Can't say that I find your implementation simple. All the weak-ref assumptions make it very verbose and hard to read. It is definitely not like canonical python code looks like. I'd say, that this is okay, if it really were necessary, but it's not. Adding a
I'm still convinced weak referencing is not the default thing to do, but a very special occasion. Especially in python, you can never rely on your observer objects being finalized at any given time. Garbage collection is undeterministic and reference counting is an implementation detail of CPython (not pypy for example). So you have to manage the observation times manually either way. Furthermore, weakrefs are available only for objects with a
Yes, I'd certainly advice you to do that, as these are conceptually really different, IMO. I'm a bit KISS/unix minded: every component should do one thing and do it well. I admit, the names
I wouldn't generalize that too much. In many cases, I'd even say removing functionality is more important than tidy code, because it can make the code conceptually easier.
Lambdas are not mind-bending but part of the language and a well-known technique to add/remove arguments (not saying it is pretty or good practice, though). I'd rather have concerns that an extra parameter is more API the user has to learn, and more importantly: an occasional reader won't understand. Don't be discouraged by my opposing opinion. I really appreciate your input which made me start to think about some of these topics for the first time as well. And in the end, there are different needs in different cases. So, maybe it's fine, that you have your own repo with your own implementation. I'd be willing to add weak references to my black-magic branch though.
I think, I can give you some more feedback on your implementation later on. Be a bit patient though. |
I had not appreciated this. If observers are always removed manually then the use case I described which lead me to consider weak references is already covered. On the other hand, I can imagine people not knowing that ref counting is specific to CPython (I didn't until just now and I've been using python for years) and assuming that their observers will go away when the ref count goes to zero. However, the obsub documentation clearly states that being an observer keeps objects alive, so users have been properly warned and I can probably retract my previous statement that weak referencing is an important addition [1]. It may be worth a simple statement in the documentation explaining this. [1] Although I maintain that it's really convenient in some cases :) |
I agree with you, that it can come in handy sometimes, and there are applications specifically developed in CPython. But even then, personally, I wouldn't want to rely on reference counting in all but the most simple cases, since it is so easy to add circular references anywhere (oops, I stored an instance method somewhere). |
There are some ideas, how one could improve on the current interface to make it more flexible. This could probably go beyond the current decorator-only implementation. The usage of the
-=
and+=
operators is also something to be discussed.From my point of view it would make sense to collect some use cases, for which this would be necessary. Then we could condense into a more concrete picture of what could change.
The text was updated successfully, but these errors were encountered: