473,289 Members | 1,848 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,289 software developers and data experts.

Cast list of objects to list of strings

I have this function:

def write_err(obj):
from sys import stderr
stderr.write(str(obj)+"\n")

and I'd like to rewrite it to take a variable number of objects.
Something like this:

def write_err(*objs):
from sys import stderr
stderr.write(" ".join(objs)+"\n")

but I lose the property that the function works on any object. What's
the simplest way to fix this? In essence, I need to cast a list of
objects to a list of strings. I'd like to do just "str(objs)" but that
(obviously) doesn't quite do what I need.

Jun 27 '08 #1
6 1799
bukzor wrote:
I have this function:

def write_err(obj):
from sys import stderr
stderr.write(str(obj)+"\n")

and I'd like to rewrite it to take a variable number of objects.
Something like this:

def write_err(*objs):
from sys import stderr
stderr.write(" ".join(objs)+"\n")

but I lose the property that the function works on any object. What's
the simplest way to fix this? In essence, I need to cast a list of
objects to a list of strings. I'd like to do just "str(objs)" but that
(obviously) doesn't quite do what I need.
I think what you want is:

def write_err(*args):
from sys import stderr
stderr.write("\n".join([str(o) for o in args]))
but then I don't really understand why you would want such a function so I could
be way wrong.

-Larry
Jun 27 '08 #2
On Jun 2, 4:02 pm, Larry Bates <larry.ba...@websafe.com`wrote:
I think what you want is:
def write_err(*args):
from sys import stderr
stderr.write("\n".join([str(o) for o in args]))
Slight nitpick. If you are using version >= 2.4 you could use a
generator expression instead of a list comprehension to avoid building
and throwing away a list.

"\n".join(str(o) for o in args)

http://www.python.org/dev/peps/pep-0289/

Very unlikely to yield a material time difference in this case but
cleaner IMO.

....
Jay Graves
Jun 27 '08 #3
bukzor wrote:
I have this function:

def write_err(obj):
from sys import stderr
stderr.write(str(obj)+"\n")

and I'd like to rewrite it to take a variable number of objects.
Something like this:

def write_err(*objs):
from sys import stderr
stderr.write(" ".join(objs)+"\n")

but I lose the property that the function works on any object.
No you don't. If you were happy with printing the str(...) of a single
objects, why not just printout the (concatenation) of the str(...) of
each of many objects?

stderr.write(" ".join([str(b) for b in objs])+"\n")

Gary Herron

What's
the simplest way to fix this? In essence, I need to cast a list of
objects to a list of strings. I'd like to do just "str(objs)" but that
(obviously) doesn't quite do what I need.

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #4
On Jun 2, 2:56 pm, jay graves <jaywgra...@gmail.comwrote:
On Jun 2, 4:02 pm, Larry Bates <larry.ba...@websafe.com`wrote:
I think what you want is:
def write_err(*args):
from sys import stderr
stderr.write("\n".join([str(o) for o in args]))

Slight nitpick. If you are using version >= 2.4 you could use a
generator expression instead of a list comprehension to avoid building
and throwing away a list.

"\n".join(str(o) for o in args)

http://www.python.org/dev/peps/pep-0289/

Very unlikely to yield a material time difference in this case but
cleaner IMO.

...
Jay Graves
Thanks! That's exactly what I was looking for.
Jun 27 '08 #5
En Mon, 02 Jun 2008 18:56:08 -0300, jay graves <ja********@gmail.com>
escribió:
On Jun 2, 4:02 pm, Larry Bates <larry.ba...@websafe.com`wrote:
>I think what you want is:
def write_err(*args):
from sys import stderr
stderr.write("\n".join([str(o) for o in args]))

Slight nitpick. If you are using version >= 2.4 you could use a
generator expression instead of a list comprehension to avoid building
and throwing away a list.

"\n".join(str(o) for o in args)

http://www.python.org/dev/peps/pep-0289/

Very unlikely to yield a material time difference in this case but
cleaner IMO.
Still nitpicking: using a generator expression in this case has no
advantage. The first thing that str.join does is to create a list out of
its argument (unless it is already a list or a tuple). In fact, a list
comprehension is faster here.

--
Gabriel Genellina

Jun 27 '08 #6
On Jun 2, 8:36 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
Still nitpicking: using a generator expression in this case has no
advantage. The first thing that str.join does is to create a list out of
its argument (unless it is already a list or a tuple). In fact, a list
comprehension is faster here.
Really! I stand corrected.

I'm not a C programmer but I peeked at the source. I see that it
makes a pass to calculate the total size and then another pass to
catenate the items, which makes sense from a memory allocation
standpoint.

To compare and contrast, I looked up the 'sum' built in and saw that
it used the iterator protocol, (PyObject_GetIter). I assume that the
other similar functions (min,max, etc) would do the same.

Thanks for setting me straight.
....
Jay
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Timothy Babytch | last post by:
Hi all. I have a list that looks like , , ] I try to make it flat one: How can I archieve such an effect with list comprehension? Two cycles did the job, but that way did not look pythonic.....
7
by: James Mcguire | last post by:
Hi, I frequently do non-initialisation type structure assignment via casting: e.g. struct s{int i,j,k;} mys; .... mys=(struct s){3,4,5};
14
by: Samuel R. Neff | last post by:
Why would you cast two strings to objects to compare them? I saw code in an MS sample on MSDN and don't get it. if ( (object)name == (object)attr.name ) { both "name" and "attr.name" are...
11
by: ssg31415926 | last post by:
I need to cast a string into an object and vice versa. At the moment, I'm using code like this: int numOfObjects = Values.Length; object objects = new object; for(int i = 0; i < numOfObjects;...
3
by: Mike Cooper | last post by:
Hello All! I am getting teh above error message on the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dgt As...
6
by: John-Arne Lillebø | last post by:
Hi. I run into this problem and i could need some help to solve it. The project is an ASP.NET Web project. Including code sample of the problem. Any idea what is causing the error message ?...
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
5
by: Jimp | last post by:
Why can't I cast List<MyObject> to ICollection<IMyObject>. MyObject implements IMyObject, and of course, List implements ICollection. Thanks
14
by: budy_ludy | last post by:
Hi All, I am new to vb .net, I have an ArrayList and i store class objects in it, and later i want to retrieve each ArrayList items and type cast to the class, How can it be done ? I used...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.