469,964 Members | 1,700 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

sending string or list to a function

Hi,

Is there a neat way to write a function that can receive either a
string or a list of strings, and then if it receives a string it
manipulates that, otherwise it manipulates each string in the list?

That is, rather than having to send a list of one member
MyFunction(['var1']), I can send

MyFunction('var1') or MyFunction(['var1','var2',var3'])

Or is this bad programming style?

What do you think?

Dec 4 '06 #1
4 2129
manstey wrote:
Is there a neat way to write a function that can receive either a
string or a list of strings, and then if it receives a string it
manipulates that, otherwise it manipulates each string in the list?
The following code shows one way you can accomplish this. I don't
consider it bad programming style to allow your functions to accept
multiple data types.

def MyFunction(val):
if isinstance(val,basestring):
val = [val]
for s in val:
#Process string
-Farshid
Dec 5 '06 #2
At Monday 4/12/2006 21:20, manstey wrote:
>Is there a neat way to write a function that can receive either a
string or a list of strings, and then if it receives a string it
manipulates that, otherwise it manipulates each string in the list?

That is, rather than having to send a list of one member
MyFunction(['var1']), I can send

MyFunction('var1') or MyFunction(['var1','var2',var3'])
That depends a bit on what you do with the argument. Sometimes it's
more clear to have two different methods, one for lists and another
for single items, specially when processing a list is *not* the same
as processing each item sequentially.
Another reason to have separate methods would be if you expect much
more calls to the single-item version than the list version.

If you want a combined version which accepts both strings and lists,
notice that unfortunately (or not!) strings and lists share a lot of
functionality. So you have to check for strings in your code, else
the first call would process 'v','a','r','1'.
That is, you usually write something like this:

def MyFunction(arg):
if isinstance(arg, basestring): arg = [arg] # or perhaps arg,
... process ...

So, if you *will* construct a list anyway, using MyFunction(['var1'])
in the first place would be better.
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
Dec 5 '06 #3
Or, just always send the function a list. If you have one string, send
it a list containing that one string.

Cheers,
-T

Dec 5 '06 #4
tl**********@gmail.com wrote:
Or, just always send the function a list. If you have one string, send
it a list containing that one string.
Or, if a single string is more common and the lists are short or generated
only for the function call, have the function accept a variable number of
arguments:
>def my_function(*items):
.... print " ".join(items)
....
>>my_function("alpha")
alpha
>>my_function("alpha", "beta")
alpha beta
>>items = ["alpha", "beta", "gamma"]
my_function(*items)
alpha beta gamma

Peter
Dec 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by intrepid_dw | last post: by
6 posts views Thread by Eduardo Rosa | last post: by
5 posts views Thread by Merrigan | last post: by
2 posts views Thread by =?Utf-8?B?QWRl?= | last post: by
7 posts views Thread by bleachie | last post: by
2 posts views Thread by Keith G Hicks | last post: by
2 posts views Thread by lstanikmas | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.