473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2268
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.