Any way to refactor this?

James Stroud jstroud at mbi.ucla.edu
Fri Apr 13 16:47:48 EDT 2007


John Salerno wrote:
> Setting aside, for the moment, the utility of this method or even if 
> there's a better way, I'm wondering if this is an efficient way to do 
> it. I admit, there was some copying and pasting, which is what prompts 
> me to ask the question. Here's the method. (I hope it looks ok, because 
> it looks really weird for me right now)
> 
> def _create_3D_xhatches():
>     for x in xrange(-axis_length, axis_length + 1):
>         if x == 0: continue
>         visual.cylinder(pos=(x,-hatch_length,0), 
> axis=(0,hatch_length*2,0), radius=hatch_radius)
>         visual.cylinder(pos=(x,0,-hatch_length), 
> axis=(0,0,hatch_length*2), radius=hatch_radius)
>         visual.cylinder(pos=(-hatch_length,x,0), 
> axis=(hatch_length*2,0,0), radius=hatch_radius)
>         visual.cylinder(pos=(0,x,-hatch_length), 
> axis=(0,0,hatch_length*2), radius=hatch_radius)
>         visual.cylinder(pos=(-hatch_length,0,x), 
> axis=(hatch_length*2,0,0), radius=hatch_radius)
>         visual.cylinder(pos=(0,-hatch_length,x), 
> axis=(0,hatch_length*2,0), radius=hatch_radius)
> 
> Since each call to cylinder requires a slightly different format, I 
> figured I had to do it this way.
> 
> Thanks.

Your parameters don't follow an unambiguous pattern. Probably best is to 
code the parameters as a set of tuples and iterate over them.

James



More information about the Python-list mailing list