sending string or list to a function

M

manstey

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?
 
F

Farshid Lashkari

manstey said:
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
 
G

Gabriel Genellina

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
 
T

tleeuwenburg

Or, just always send the function a list. If you have one string, send
it a list containing that one string.

Cheers,
-T
 
P

Peter Otten

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:
.... print " ".join(items)
....
my_function("alpha") alpha
my_function("alpha", "beta") alpha beta
items = ["alpha", "beta", "gamma"]
my_function(*items)
alpha beta gamma

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top