Stackful
Upcoming SlideShare
Loading in...5
×
 

Stackful

on

  • 951 views

A lightning talk on Stackful, a pure-Python implementation of dynamically scoped variables for Python

A lightning talk on Stackful, a pure-Python implementation of dynamically scoped variables for Python

https://github.com/erikrose/stackful

Statistics

Views

Total Views
951
Views on SlideShare
950
Embed Views
1

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 1

http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • I’m Erik Rose.\nI work at Mozilla.\nAnd I think programming should be fun.\n\nI had an unreasonable amount of fun writing stackful, an experiment in adding a new language feature in pure Python.\n\nBut first, a bit of a disclaimer…\n
  • Done basically to see if I could do it. As a bonus, useful.\nThe mission? Escape from global config in other ppl’s frameworks: a location of a DB, timeout behavior on a socket, a pathname for some file.\nI’ve found myself reaching over the course of years for a way to make exceptions to these assumptions of globalness.\nDynamic vars give us this ability.\n
  • For example (explain)\n\nYou might imagine how you can use this to shadow an undesired global for a limited time\n\nstackful lets you do this in Python…\n
  • (describe)\n\nAs you would hope, it is not only per stack frame but also per thread, so you can do this in web servers or other multithreaded programs.\n\nImplementation tricky. Main use case for stackful is other ppls libs which aren’t factored the way I want. So didn’t want attr access like threading.local, because it would break existing code. At this point, I was sad, because can’t intercept symbol reference.\n\nHowever, then it occurred to me that you don’t need to; it doesn’t have to manifest until you actually do something with it. So, it was a simple matter of overriding…\n
  • \n
  • (outer() calls middle, which calls inner()…)\n\n__add__, and inner() prints 10\n\nYou can even stack additional values on top of stackful variables.\n\nStackful is handy for things like Django settings, which are globals, or even for stdlib modules which store config values in module-level globals (socket lib timeout, I’m looking at you).\n
  • JI thub\n\nTest coverage is pretty decent, though I have a few more magic methods to test and implement.\n\nShould be able to use it. Let it be on your own head if you do. Most importantly, I had fun writing it, and I hope you had fun hearing about it.\n

Stackful Stackful Presentation Transcript

  • stackfulDynamically scoped variables for Python A ridiculous idea by Erik Rose erik@mozilla.com @ErikRose
  • <hack>
  • <hack>
  • In Perl…sub first { local $x = 1; my $y = 1; second();}sub second { print "x=", $x, "n"; # Prints x=1. print "y=", $y, "n"; # Doesnt.}first();http://perl.plover.com/local.html
  • In Perl…sub first { local $x = 1; my $y = 1; second();}sub second { print "x=", $x, "n"; # Prints x=1. print "y=", $y, "n"; # Doesnt.}first();http://perl.plover.com/local.html
  • In Python…from stackful import stackfulwith stackful(some_global, 8): foo()
  • https://github.com/erikrose/stackful erik@mozilla.com @ErikRose