472,371 Members | 1,601 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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 2204
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
6
by: Eduardo Rosa | last post by:
Somebody knows how I queue email using .Net? thanks a lot
5
by: Merrigan | last post by:
Hi, I have now eventually finished my newbie-backup script and I'm very proud of the way it functions... Anyways, I am looking for an easy way to use smtplib to send an email with the output...
1
by: xin.yadong | last post by:
Hi: I have a shared function for sending Email using SMTP. It works fine in a ASP.NET web application. But when I use it in a VB.Net Windows application, it always gave me an error: "Could not...
2
by: =?Utf-8?B?QWRl?= | last post by:
HI All, I am encountering the following error when I try to send an email through a SMTP server. I believe the problem lies with the authentication part when the network crednetials are used,...
0
by: damimkader | last post by:
Hi, I'm trying to send emails using a Macro based on an Excel Sheet and the Email Client I'm using is Lotus Notes. OS used - Windows Xp. Language - VB Below is the Code I'm using for doing...
7
by: bleachie | last post by:
Hey, I just need some help, my form seems to not send me all of the 'guestNames' and 'guestEmails' forms. i use this function to add more guestnames and guestemail fields based on the number of...
2
by: Keith G Hicks | last post by:
I'm using the following code to send out email messages to a list of people in a database. My problem is that if I'm sending to 100 people and the 40th address is bad, it crashes on that one and...
2
by: lstanikmas | last post by:
Hi, I'm validating a form with this ASP but receiving some blank email responses; does anyone see anything wrong with it?: function isFormVarExcluded(thisForm, strToCheck) { var strExcludeVars...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.