[Chicago] Inline modules declarations? [tangent off of "Fw: using CruiseControl for non-java projects?"]

Jason R Huggins JRHuggins at thoughtworks.COM
Mon Oct 9 21:16:46 CEST 2006


Ian Bicking wrote:
> PEP 359 (http://www.python.org/dev/peps/pep-0359/) could allow this. 
> Unfortunately its been withdrawn, though it still comes up over and 
> over.  It would be nice if someone took it up again, as it still has 
> fans (myself included, though I don't track py-dev enough to become its 
> champion, at least not right now).
> 
> Anyway, with it you'd probably do:
> 
> make Build my_build:
>      def clean():
>          ...
>      make Build communicate:
>          def send_email(): ...
> 


Reminds me very, very much of the "with" statement. Wonder why he liked 
"with", but not "make". I'm probably not understanding the semantics 
correctly though.

The point of my idea is that there is no explicit declaration of "def", 
"class", "with", or "make". It makes the source cleaner and nicer to look 
at... And it doesn't even feel like programming syntax ( a feature a like 
about Ruby by virtue of its optional parens on method calls.)

But...

build:
    clean:
        <do stuff>
    checkout:
        <do stuff>
    test:
        <do stuff>
    communicate:
        <do stuff>

... does violate the "explicit is better than implicit" rule, but on the 
other hand, it's more beautiful than "def", "class", "with", and "make" 
declarations.

I see two downsides with my proposal, though.
One, because they're just modules in the namespace, they aren't ordered 
(well they are technically ordered, but Python re-orders the namespace 
alphabetically on import)... YAML solves this by adding hyphen syntax for 
explicit ordering:

build:
    - clean:
    - checkout:
    - test:
    - communicate:


Second problem... because 99% of the time, modules are real folders and 
files on the filesystem, each needs to be named uniquely. So this wouldn't 
be possible:

html:
    body:
        div:
      div:
 
But this would:
html:
    body:
        with div():
            <div stuff>
        with div():
            <more div stuff>


or this would be okay:
html:
    body:
        div-1:
            each_module_has_a_unique_name = True
        div-2:
              though_numbering_would_be_tedious = True



> 
> Anyway, the actual mechanics are pretty simple:
> 
> my_build = Build("my_build", (), {'clean': clean function, 
> 'communicate': Build('communicate', (), {'send_email': send_email 
> function})})

Yup. Since the translation is easy enough, I might just write a 
"translator" that converts my "source as YAML+code" into "virtual 
modules+code" at runtime. But it would be nice if inline modules were 
baked in from the start.

> It's for making declarative name-based structures easier to build; kind 
> of like Lisp's S-Expressions, except where S-Exps are about nested 
> tree-like structures, these are based more on dictionaries.

:-) 2 things every Python programmer needs to do in life: 1) Reinvent Lisp 
2) Write a web framework

-Jason



More information about the Chicago mailing list