rohit
Comments
Reactions

Aren't django signals a little like comefrom?

By : rohit


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](http://en.wikipedia.org/wiki/COMEFROM)

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?

--------------
* [There is a library to add goto and comefrom to Python](http://entrian.com/goto/)
* [Discussion about COMEFROM](http://www.c2.com/cgi/wiki?ComeFrom)

-------------
You should follow me on [twitter here](http://twitter.com/uswaretech).

Related Posts


Can we help you build amazing apps? Contact us today.

Topics : django rambling

Comments

mjc 19th July, 2009

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.

commmenttor
K 19th July, 2009

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 :-)

commmenttor
Douglas 19th July, 2009

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();

commmenttor
Max Battcher

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.

commmenttor
bill jones

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.

commmenttor
悉尼

yes, it's a little bit like comefrom...

commmenttor
Gonzalo Delgado

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

commmenttor
Kelvin71

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. ,

commmenttor
Jonathan Hartley

Signals are useful if the sender should not know about the variety of recipients who might be interested in the event.

For example, if you have a renderer which is dependent on your model, and it should update itself whenever the model changes. The model can fire a signal when it changes, and the renderer will receive that signal.

This means that the model has no dependency on the renderer - so we could change or replace the renderer, or use several different ones at the same time, without the model ever having to change.

commmenttor
Lily

That what i was looking for.I tried your code but did not workError:AttributeError at /accounts/login/ AnonymousRequired' ojcbet has no attribute __name__'Request Method: GETRequest URL: Exception Type: AttributeErrorException Value: AnonymousRequired' ojcbet has no attribute __name__'Exception Location: C:\Python26\lib\functools.py in update_wrapper, line 33Python Executable: C:\Python26\python.exePython Version: 2.6.0

commmenttor
Bonne r¨¦putation pour Enti¨¨rement New Balance Chaussures de jogging

Aren't django signals a little like comefrom? - Agiliq Blog | Django web app development

commmenttor
http://www.melikepinar.k12.tr/honornewshoes/ADIDAS/lMvieFB5aP/

焼肉うしごろがプロデュースする 3つのフロアに分かれたBambinaの集大成

commmenttor
prada candy tester

Pensacola Has The nickname "The City of Five Flags." Only the Pensacola Chamber of Commerce That name actually uses, but it's a convenient short-handed way of Describing the city's history. Over the past 450 years, Pensacola Has Been owned by five nations: Spain, France, the United Kingdom, the United States, and the Confederate States. Also It was inhabited by various Native American tribes, but never sloganeers Seem to include them.

commmenttor
venus factor

Hi there! Quick question that's totally off topic. Do you know how to make your site mobile friendly? My site looks weird when viewing from my iphone. I'm trying to find a template or plugin that might be able to correct this issue. If you have any recommendations, please share. Appreciate it!

commmenttor
the venus factor

This design is wicked! You certainly know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost...HaHa!) Great job. I really loved what you had to say, and more than that, how you presented it. Too cool!

commmenttor
venus factor

I've loaded your website in Several totally different internet browsers and I must say this website loads a lot faster then most. Would you mind e-mailing me the company name of your website hosting company? My personal email is: %EMAIL%. I'll even sign up through your affiliate link if you would like. Cheers

commmenttor
venus factor

Hello. I was considering adding a backlink back to your blog since both of our websites are centered around the same topic. Would you prefer I link to you using your site address: %BLOGURL% or blog title: %BLOGTITLE%. Please make sure to let me know at your earliest convenience. Thank you

commmenttor
the venus factor reviews

Howdy! This is kind of off topic but I need some advice from an established blog. Is it difficult to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about creating my own but I'm not sure where to start. Do you have any points or suggestions? Thanks

commmenttor
the venus factor

Amazing! I'm truly enjoying the design of your web site. Are you using a customized theme or is this readily available to all individuals? If you do not want to say the name of it out in the public, please contact me at: %EMAIL%. I'd love to get my hands on this theme! Thanks.

commmenttor
eat stop eat pdf

Howdy just wanted to give you a brief heads up and let you know a few of the images aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different internet browsers and both show the same results.

commmenttor
venus factor

Your weblog appears to be having some compatibilty problems in my chrome browser. The content appears to be running off the webpage pretty bad. If you would like you can contact me at: %EMAIL% and I'll shoot you over a screen grab of the problem.

commmenttor

Reactions

uswaretech

Aren’t django signals a little like comefrom? New Post. http://bit.ly/tzHa4

This comment was originally posted on Twitter

webstartupgroup

StartupNews: Aren’t Django signals a little like COMEFROM? http://bit.ly/TjE7b

This comment was originally posted on Twitter

Zak

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

ubernostrum

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

shabda

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

pistoriusp

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

Confusion

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

thedz

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

mahmud

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

This comment was originally posted on Hacker News

DougBTX

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

Zak

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

null_vector

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

This comment was originally posted on Reddit

Post a comment Name :

Email :

Your site url:

Comment :

© Agiliq, 2009-2012