We love designing and developing websites, but what really drives us is solving problems and cultivating strong relationships with our clients
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).
Comments
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 :-)
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();
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.
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.
So you're saying the observer pattern is a bit like comefrom.. interesting.
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. ,
Reactions
Aren’t django signals a little like comefrom? New Post. http://bit.ly/tzHa4
This comment was originally posted on Twitter
StartupNews: Aren’t Django signals a little like COMEFROM? http://bit.ly/TjE7b
This comment was originally posted on Twitter
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
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
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
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
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
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
CL’s customizable generic dispatch is just more than :before :-) the whole thing is just tasty.
This comment was originally posted on Hacker News
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
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
Aren’t functions calls a little like COMEFROM and then a GOBACK?
This comment was originally posted on Reddit
- Test Driven Development in Python
- Deploying Django apps on Heroku
- Developing android applications from command line
- Deploy Django App in 5 Easy Steps
- Project Management Tools for Start-Ups
- Generating a pdf from an image using PIL and django
- Dynamically attaching SITE_ID to Django Caching
- Screencast: How to deploy Django on Heroku
- Deploying Django apps on Heroku
- How to use pep8.py to write better Django code
- rails
- django
- linkroundup
- django opinion
- opinion
- business
- API
- appengine
- python
- satire
- startup
- Uncategorized
- marketing
- personal
- rambling
- search
- interviews
- seo-interviews
- 5startupideas
- ideas
- seo
- tips
- forms
- paypal
- utilities
- datetime
- web2.0
- Amazon
- algorithms
- presentations
- products
- pinax
- satchmo
- ecommerce
- microsoft
- yahoo
- book
- tutorial
- models
- aggreagtion
- meta
- India
- apps
- about
- CSS
- Design
- wordpress
- test slug
- vim
- urls
- reviews
- javascript
- xmpp
- emacs
- Typography
- Grid Theory
- Color Theory
- iphone
- android
- titanium
- mobile applications
- CSS3
- Browser Compatibility
- mobile
- jobs
- lamson
- django setup
- files
- upload
- jsTree
- hierarchical view
- web page
- Treeview
- coffeescript
- request
- response
- South
- django south
- django migration
- --fake
- screencasts
- django caching
- SITE_ID prefix
- review
- code hosting
- comparison
- unfuddle
- fogbugz
- assembla
- github
- project management
- ticketing system
- gunicorn
- deploy
- nginx
- ubuntu
- vps
- android terminal
- terminal
- programming
- TDD
- Test Driven
- Development
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- October 2011
- September 2011
- July 2011
- June 2011
- April 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- June 2010
- April 2010
- March 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- April 2009
- March 2009
- February 2009
- November 2008
- October 2008
- June 2008
- May 2008
- April 2008
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.