[Q] python vs checker.py maxlocals

achrist at easystreet.com achrist at easystreet.com
Wed Mar 26 18:55:12 EST 2003


sismex01 at hebmex.com wrote:
> 
> argh... 60 locals in a single function???
> 
> Most probably, it isn't that Python won't allow more,
> it's that it's very bad form to do such a thing.
> Can't you break it up into smaller functions which
> so a very concise, small task?
> 

No, I think that I've got the code structured ok. Run-on code
is ok by me if you are doing the same thing predictably and 
repeatedly with columns that line up. I've got a pre-defined
datastructure that I'm building.  Something like this hypothetical
example:

    universe   	= tree.AddRoot("The Universe")

    milkyWay    = AddTreeItem( tree,  universe,    "Our Galaxy" )

    solarSystem = AddTreeItem( tree,  milkyWay,    "Solar System" )

    earth       = AddTreeItem( tree,  solarSystem, "Earth" )

    kansasCity  = AddTreeItem( tree,  earth,       "Kansas City" )

  ... and so on ad nauseum

My tree has ~60 nodes now, how many ultimately, IDK. Since I'm just
noodling around with the tree, and will be adding and deleting
nodes, re-arranging the hierarchy, etc, it looks to me to be
simpler and less unmaintainable with named nodes and connections
as above instead of:

    d           = {}
    d["universe"]   	= tree.AddRoot("The Universe")
    d["milkyWay"]    = AddTreeItem(tree,  "universe",    "Our Galaxy")
    d["solarSystem"] = AddTreeItem(tree,  "milkyWay",    "Solar
System"   
    d["earth"]       = AddTreeItem(tree,  "solarSystem", "Earth")
    d["kansasCity"]  = AddTreeItem(tree,  "earth", "Kansas City")
    ...

This has the slight disadvantage that the string literals have to be
typed in twice, introducing a slightly different failure mode in the
event of a typo.

Or writing it out in some lisp-like notation:

    tree = initializeTree( 		
		[ "The Universe", 
		   [ "Our Gslaxy",
		   	["Solar System",
			    [ "Earth",
				[ "Kansas City"]
			    ]
			]
		   ]
		]	
	   ) 					

This is nice in that there is no repetition, and if I typo, I'll get a
syntax error, but if I like reading lisp, I've got some real problems
doing this in Python.

Or XML (shudder)?  This is not data transport, it's just a program.

Is there any more pythonic idiom for this?  There is one additional
complexity to this that I omit above for simplicity:  The tree is a
wxPython wxTreeCtrl, and there is an additional parameter used in the
construction  of each node ... a user data item that contains a
function to call when the coresponding tree item is selected by the
user. I really don't want this to be defined outside the program in
some file where things can get fouled up after I've got it ok.

TIA for any idioms for the idiosyncratics.


Al





More information about the Python-list mailing list