Type of regular expression

Aahz aahz at pythoncraft.com
Tue Jul 6 15:55:41 CEST 2004


In article <4ywu1mu8nu.fsf at skjellgran.ii.uib.no>,
Joakim Hove  <hove at bccs.no> wrote:
>
>Thanks for answering, however I am afraid i posed the question
>somewhat ambigously: The point is that i want the function to do
>different things depending on the type of input:
>
>def do_something(arg):
>    if type(arg) is RegexpType:
>       # Handle regular expression argument
>    elsif type(arg) is StringType:
>       # Handle string argument - which is something
>       # completely different.
>    else:
>       sys.exit("Wrong argument type")

That's a Bad Idea.  What if someone gives you a Unicode string?
Generally speaking, type-based dispatch will run into problems sooner or
later.  The better approach: you have to know at some point what kind of
data is being passed around -- when you create a target (usually a name)
for it.  If you really need to know the type later on, create a
container that includes the necessary type information.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith, c.l.py



More information about the Python-list mailing list