Implicit lists

Christian Tismer tismer at tismer.com
Sat Feb 1 12:27:57 EST 2003


Erik Max Francis wrote:
> Max M wrote:
> 
> 
>>You will need to check if the argument is a list or a string in all
>>the
>>code wich calls one of the function.
>>
>>if type(options) == type(''):
>>     getWhereByOne(options)
>>else:
>>     getWhere(options)
>>
>>This is no better than the first option.
> 
> 
> Why do you _have_ to check?  You're doing precisely the thing that
> you're not supposed to do in dynamic languages; you're limiting the
> types that can be passed to them unnecessarily.
> 
> Having two interfaces where one takes "a thing" and the other takes "a
> sequence of things" is totally reasonable, since one will be treated as
> things, and the other will be treated as a sequence of things.
> 
> In your specific case those things are strings, but that analysis
> doesn't generalize fully.

You are right, in general. The problem is simply that
strings can be treated as objects or sequences all
the time, and that a common error is to pass a string
in a sequence context, and your function iterates over
the single chars. This especially hurts when your
function expects a list of filenames, and suddenly it
creates a bunch of single char named files :-)

The problem with this polymorphy is that the code has
to decide what the user most probably has thought.
In this context, it would be nice to be able to spell
"this parameter is a string, nothing else". But there
is no simple way to do this.
To be able to draw a clear decision about this, we
wouzld need a property/method/whatever that tells
us what the intent of this parameter usage has been.
Unfortunately, there seems to be no simple way.

ciao - chris






More information about the Python-list mailing list