Aren’t django signals a little like comefrom?

by rohit on July 18, 2009

In computer programming, COMEFROM (or COME FROM) is an obscure control flow structure used in some programming languages, primarily as a joke.

I never liked using signals. Recently I was asked that, and has no answer, but a little thinking led me to this question. Aren’t signals a little like COMEFROM. If yes, aren’t they as bad as COMEFROM? If you do not know what a COMEFROM is, wikipedia to the rescue

Some hypothetical code using COMEFROM, again from wikipedia,

from goto import comefrom, label
comefrom .repeat
name = raw_input('what is your name? ')
if name:
    print "Hello",name
    label .repeat
print "Goodbye!"

And Some actual Django code using signals

class Post(models.Model):
    name = models.CharField(...)
    ...
    num_comments = models.PositiveIntegerField(default = 0)

class Comment(models.Model):
    ...
    post = models.ForeignKey(Post)

def handle_num_comments(sender, **kwargs):
    instance = kwargs['instance']
    instance.post.num_comments+=1
    instance.post.save()

from django.db.signals import post_save

post_save.connect(handle_num_comments, sender=Comment)

And the same code using COMEFROM

class Post(models.Model):
    name = models.CharField(...)
    ...
    num_comments = models.PositiveIntegerField(default = 0)

class Comment(models.Model):
    ...
    post = models.ForeignKey(Post)

    def save(self, *args, **kwargs):
        super(Comment, self).save(*args, **kwargs)
        instance = self
        LABEL .post_save

def handle_num_comments(sender, **kwargs):
    instance = kwargs['instance']
    COMEFROM .post_save
    instance.post.num_comments+=1
    instance.post.save()

So isn’t the signals code a little like COMEFROM, and why is it superior to COMEFROM?



You should follow me on twitter here.

Related posts:

  1. Doing things with Django forms

1 Comment 2 Tweets 9 Comments

{ 20 comments… read them below or add one }

1 mjc July 19, 2009 at 12:11 am

I remember in the 70′s studying the semantics of programming languages and coming across this definition of the semantics of a label: The condition true at a label is the union (logical or) of all the conditions true at the places where the next statement (via goto or falling through) is the label. This seemed (1) true after it is pointed out, and (2) a lot like the COMEFROM.

2 K July 19, 2009 at 4:09 am

I haven’t looked at Django, but it seems like basic event handling to me. If so, you could say that all event handling is a lot like COMEFROM :-)

3 Douglas July 19, 2009 at 8:33 am

The difference between COMEFROM and event handling/post_save, is that control does not return back to the label after COMFROM.

Example: register_post_save_handler(); save(); // handler called here now_this_is_called();

register_COMEFROM_post_save(); save(); // handler called here this_may_not_get_called,_call_stack_gone();

4 Max Battcher July 19, 2009 at 4:57 pm

Although “GOTO is considered harmful”, no one has proven that COMEFROM is harmful. In the case of INTERCAL, COMEFROM is a nightmare because it is the only real flow-control mechanism in the language and requires some mind-bending to actually accomplish more straightforward patterns. Django signals doesn’t suffer from the same problems as COMEFROM, because ultimately it is not the only flow-control mechanism in Django/Python. Signals are merely a nice aspect-oriented/event-oriented support tool that is one in a large bag of useful tools.

5 bill jones July 19, 2009 at 5:26 pm

The similarity is superficial.

COMEFROM (and GOTO) are both static – in that they are defined when the code is written and cannot be changed at runtime. Signal-based connections can be created or removed at runtime.

Any number of signal handlers can respond to a single event, whereas COMEFROM/GOTO only transfer control flow from point A to point B (not B1, B2… Bn).

Also, I’m pretty sure your COMEFROM based translation of the signal example could not possibly work. For example, what’s the actual entry point to the ‘handle_num_comments’ function? The fact that you can’t create a corresponding example (that actually works) kind of proves the point that these structures are not equivalent.

6 悉尼 July 19, 2009 at 8:56 pm

yes, it’s a little bit like comefrom…

7 Gonzalo Delgado July 19, 2009 at 9:20 pm

So you’re saying the observer pattern is a bit like comefrom.. interesting.

8 Kelvin71 October 22, 2009 at 9:08 pm

Ask yourself these seven questions to determine what type of learner you are: If you would choose to read about it you are more of a visual learner, if you would prefer to hear about it you are a more auditory learner and if you would like to try it yourself you are more of a tactile learner. ,

11 Zak July 19, 2009 at 12:07 am

It’s better because it doesn’t require messing around with, or even being aware of the internals of the code it’s being connected to. It’s actually a lot like a Common Lisp :before method, which I’ve found really bloody useful on a few occasions.

This comment was originally posted on Hacker News

12 ubernostrum July 19, 2009 at 1:57 am

I’ve always just looked at them the way I look at any other event-based hooks — they’re a way to say "when this thing happens, I want to know about it and run some code".Some programming languages even have this built in :)

This comment was originally posted on Hacker News

13 shabda July 19, 2009 at 2:18 am

But doesn’t it lead itself to "Action at distance" anti pattern?Like everyone else, we use django-regsitration a lot. Here in your code, http://bitbucket.org/ubernostrum/django-registration/src/tip…; you send a custom signal. Now any one can break the normal flow of control and call control _from any where in the project to anywhere in the project_.

This comment was originally posted on Hacker News

14 pistoriusp July 19, 2009 at 2:38 am

I think it’s fine to say that you don’t like signals. But I wouldn’t do so without proposing a better alternative.

This comment was originally posted on Hacker News

15 Confusion July 19, 2009 at 3:32 am

No they aren’t. They are an implementation of the template method pattern, perhaps with some cross polination from an observer pattern.

This comment was originally posted on Hacker News

16 thedz July 19, 2009 at 4:54 am

Signals defines a fairly concise interface for interacting with events.You’re not "stealing" control from the loop. Rather, think of it more as listening and attaching something to an event.

For me, it helps to think of signals less as interrupts in a traditional program loop, but as something more akin to events in event-driven environments.

This comment was originally posted on Hacker News

17 mahmud July 19, 2009 at 8:27 am

CL’s customizable generic dispatch is just more than :before :-) the whole thing is just tasty.

This comment was originally posted on Hacker News

18 DougBTX July 19, 2009 at 8:40 am

break the normal flow of controlBut after the signal is fired, and handlers do their handling, normal flow is restored and the code just after the signal resumes (assuming no exceptions were thrown). COMEFROM doesn’t make that guarantee, making it much harder to follow.It isn’t that code can jump to an arbitrary point in the program (a function call can do that) but that the code after that point won’t get called, barring a corresponding GOTO back to the label.

This comment was originally posted on Hacker News

19 Zak July 19, 2009 at 2:11 pm

Indeed. If I ever design a general-purpose language, you can bet on it having something of that sort.

This comment was originally posted on Hacker News

20 null_vector July 21, 2009 at 7:12 pm

Aren’t functions calls a little like COMEFROM and then a GOBACK?

This comment was originally posted on Reddit

Leave a Comment

Additional comments powered by BackType

Previous post:

Next post: