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
© Agiliq, 2009-2012