473,406 Members | 2,336 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,406 software developers and data experts.

sequence of objects

What is the recommended way to create a sequence of objects? Assume the
objects have a default constructor:

class X:
def __init__(self):
something

This doesn't work:
a = 64*(X(),)

This creates a tuple of 64 copies of a single identical object, not 64
instances of X.

I could create an empty list, then call append (X()) 64 times, but that's
not very eligant (or maybe efficient). Any suggestions?

Jul 18 '05 #1
2 1080

a = [X() for i in xrange(60)]

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Neal D. Becker <nd*******@verizon.net> wrote:
What is the recommended way to create a sequence of objects? Assume the
objects have a default constructor:

class X:
def __init__(self):
something

This doesn't work:
a = 64*(X(),)

This creates a tuple of 64 copies of a single identical object, not 64
instances of X.

I could create an empty list, then call append (X()) 64 times, but that's
not very eligant (or maybe efficient). Any suggestions?


I suspect you can't do much better than a list comprehension:

a = [X() for i in xrange(64)]

A _little_ better, maybe, with itertools:

import itertools as it

a = [X() for i in it.repeat(0,64)]

or even:

a = list(it.starmap(X, it.repeat((), 64)))
The itertools-based solutions DO go faster on my machine (at least with
Python2.4):

kallisti:~/cb alex$ python2.4 timeit.py -s'class X:pass' -s'import
itertools as it' '[X() for i in xrange(64)]'
1000 loops, best of 3: 208 usec per loop

kallisti:~/cb alex$ python2.4 timeit.py -s'class X:pass' -s'import
itertools as it' '[X() for i in it.repeat(0,64)]'
10000 loops, best of 3: 168 usec per loop

kallisti:~/cb alex$ python2.4 timeit.py -s'class X:pass' -s'import
itertools as it' 'list(it.starmap(X, it.repeat((), 64)))'
10000 loops, best of 3: 138 usec per loop

However, unless you're really squeezed for cycles (which is why I'm
doing the measurements with 2.4: if you ARE squeezed, upgrading to 2.4
and its generally better speed may be urgent for you), the simplicity of
the list comprehension may probably be preferable to these speedups (at
20% or even 35%, they sure look yummy, but remember, this is with a
class without even an __init__ -- add an __init__ doing any substantial
work, and the relative % speedup will decrease).
Alex
Jul 18 '05 #3

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

Similar topics

5
by: Xela | last post by:
Hi! I would like, without writing a procedure which require the configuration of an additionnal C-compiler and which is not portable (I am wring à java application targeted on DB2). And I would...
5
by: sandravandale | last post by:
I want to be able to pass a sequence (tuple, or list) of objects to a function, or only one. It's easy enough to do: isinstance(var, (tuple, list)) But I would also like to accept...
1
davydany
by: davydany | last post by:
Hey guys...a n00b Here for this site. I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the...
5
by: Anan18 | last post by:
Hello sir, I'm supposed to Implement and Test the sequence Class Using a Fixed-Sized Array (Chapter 3), from Data Structures & Other objects using c++. The header file is provided, and so is a test...
21
by: bilgekhan | last post by:
After doing a succcessful insert() or find() on a set<Tcontainer is it possible to get the item number of this item in the set? (ie. its zero-based sequence number (position/location/rank/index)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.