I have a problem guys. It's due to duck typing. Now I expected to run into something like this sooner or later, but I can't help but feel there's a better solution.
import re def patternMatching(pattern, string): matchList = re.findall(pattern, string) print '\n'.join(['%s' % v for v in matchList])
If you run that function as patternMatching(r'The (fox)', 'The fox'), it gives you 'fox', right? Because matchList is .
But what if you run it as patternMatching(r'(The) (fox)', 'The fox')? You get an error. Because matchList is [('The','fox')].
Duck typing is messing with my types, so the print function won't work.
Traceback (most recent call last): File "<pyshell#52>", line 1, in <module> print '\n'.join(['%s' % v for v in matchList]) TypeError: not all arguments converted during string formatting
What's a programmer to do? My patch solution is to check the type of the first element and then do two cases. But that won't hold if I get something like [('hello','bye'),'hello']... which I'm not sure I ever will, but the point remains.