Arnaud Delobelle schrieb: > On Mar 7, 8:52 pm, Larry Bates <[EMAIL PROTECTED]> wrote: > [snip] >> Without knowing more about the functions and the variable it is somewhat >> hard to tell what you are trying to accomplish. If a, b, c are functions >> that act on x when it is a different type, change to one function that >> can handle all types. > > I'm not really thinking about this situation so let me clarify. Here > is a simple concrete example, taking the following for the functions > a,b,c I mention in my original post. > - a=int > - b=float > - c=complex > - x is a string > This means I want to convert x to an int if possible, otherwise a > float, otherwise a complex, otherwise raise CantDoIt. > > I can do: > > for f in int, float, complex: > try: > return f(x) > except ValueError: > continue > raise CantDoIt > > But if the three things I want to do are not callable objects but > chunks of code this method is awkward because you have to create > functions simply in order to be able to loop over them (this is whay I > was talking about 'abusing loop constructs').
In your case, I don't consider it an abuse - au contraire. Because in such a situation where a possibly growing number of functions dealing with one value until one of them "fits" a loop is the natural thing to do, as it won't change in appearance just because you add a new conversion-function to some (semi-)global list. I'd consider it especially good style in that case. Diez -- http://mail.python.org/mailman/listinfo/python-list