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
- Easy client side form validations for Django: Django Parsley
- Two Scoops of Django: Review
- Serving static files in Django
- Logging in Django
- Common testing scenarios for Django app.
Can we help you build amazing apps? Contact us today.
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.