Draft Pep

Lulu of the Lotus-Eaters mertz at gnosis.cx
Wed Aug 7 09:27:52 CEST 2002


huaiyu at gauss.almadan.ibm.com (Huaiyu Zhu) wrote previously:
|def func1 ...
|def func3 ...
|def func4 ...
|callbacks = {
|   "case a": func1,
|   "case b": lambda ...
|   "case c": func3,
|   "case d": func4,
|   "case e": lambda ...
|}
|Pulling functions out of data structures not only pollutes module name
|spaces with unwanted names, but also splits semantically connected material
|to separate locations in the code.

Sure.  But this problem can be 90% mitigated by using a function-level
scope, IMO:

    def mk_callbacks(dct={}):
        def func1: ...
        dct["case a"] = func1
        dct["case b"] = lambda ...
        def func3: ...
        dct["case c"] = func3
        def func4: ...
        dct["case d"] = func4
        dct["case e"] = lambda ...
        return dct

You can either save the dictionary with 'callbacks = mk_callbacks()', or
just use the function direction as its dictionary:

    mk_callbacks()[somevar]()

My example might not be completely perfect, but there's no pollution...
and it avoids the IMO far uglier consequences of the PEP.

Yours, Lulu...

--
 mertz@   _/_/_/_/_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY:_/_/_/_/ v i
gnosis  _/_/                    Postmodern Enterprises         _/_/  s r
.cx    _/_/  MAKERS OF CHAOS....                              _/_/   i u
      _/_/_/_/_/ LOOK FOR IT IN A NEIGHBORHOOD NEAR YOU_/_/_/_/_/    g s





More information about the Python-list mailing list