472,353 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

pretty basic instantiation question

i'm very new to python, but i have a pretty basic question:
let's say i have a class, and i need to create a different number of
instances (changes every time - and i can't know the number in advance) in
a loop.
a function receives the number of instances that are needed, and creates
them like,
a=Myclass()
b=Myclass()
..
..
..
..
..
..

what's the easiest way to go about this?

thanks,
A

Oct 23 '06 #1
5 889
si*****@lkb.ens.fr wrote:
let's say i have a class, and i need to create a different number of
instances (changes every time - and i can't know the number in advance) in
a loop.
a function receives the number of instances that are needed, and creates
them like,
a=Myclass()
b=Myclass()
def create_instances(n):
return [Myclass() for i in xrange(n)]
Oct 23 '06 #2
Leif K-Brooks wrote:
si*****@lkb.ens.fr wrote:
>>let's say i have a class, and i need to create a different number of
instances (changes every time - and i can't know the number in advance) in
a loop.
a function receives the number of instances that are needed, and creates
them like,
a=Myclass()
b=Myclass()


def create_instances(n):
return [Myclass() for i in xrange(n)]
Leif's point being you *don't* want to bind a different name to each of
a variable number of things - that way madness lies, as you end up
creating Python statements on the fly using eval() and exec and other
such dangerous and insanity-inducing tricks :-)

Instead use a container structure like a list to hold them, and then use
an appropriate technique to access them while they are still in the
container.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 23 '06 #3
Why do you need to know the number of instances. I know that Python
does not support Class Variable, but you can always create a global
variable and increase it whenever you add a new instance. this will
keep a record for you of how many instances you have.

si*****@lkb.ens.fr wrote:
i'm very new to python, but i have a pretty basic question:
let's say i have a class, and i need to create a different number of
instances (changes every time - and i can't know the number in advance) in
a loop.
a function receives the number of instances that are needed, and creates
them like,
a=Myclass()
b=Myclass()
.
.
.
.
.
.

what's the easiest way to go about this?

thanks,
A
Oct 24 '06 #4
On Mon, 23 Oct 2006 17:36:14 -0700, meithamj wrote:

[fixing top-posting]
si*****@lkb.ens.fr wrote:
>i'm very new to python, but i have a pretty basic question:
let's say i have a class, and i need to create a different number of
instances (changes every time - and i can't know the number in advance) in
a loop.
As others have pointed out, the answer is "don't do that, use a list of
instances instead".
Why do you need to know the number of instances. I know that Python
does not support Class Variable, but you can always create a global
variable and increase it whenever you add a new instance. this will
keep a record for you of how many instances you have.
That's not what the Original Poster asked for, but there is a better way
than keeping a global variable. Make the counter a class attribute. That
way you don't have to increment the global, the class does its own
counting.
class CountedClass:
count = 0
def __init__(self, arg):
self.__class__.count += 1
self.arg = arg
def __del__(self):
self.__class__.count -= 1
Example:
>>a = CountedClass(4)
b = CountedClass(4)
c = CountedClass(4)
CountedClass.count
3
>>a = 1
CountedClass.count
2
>>del b
CountedClass.count
1
>>L = [CountedClass(None) for i in range(1000)]
CountedClass.count
1001

--
Steven

Oct 24 '06 #5
me******@gmail.com wrote:
Why do you need to know the number of instances. I know that Python
does not support Class Variable,
Plain wrong.

class Class(object):
classvar = ["w", "t", "f"]

print Class.classvar

c = Class()
c.classvar.append("???")
print c.classvar
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 24 '06 #6

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

Similar topics

7
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
2
by: David | last post by:
Assuming of course that the instantiation statement is 100% ok. As a matter of fact, that very instantiation passes successfuly on VC++ 7.1 (and...
5
by: ESOJAY | last post by:
My native language is c, but I have the unpleasant task of porting a C++ code from SGI MIPS to Linux Opteron, I have no help from the author of the...
13
by: Winbatch | last post by:
Hi, If this should be directed to another group, please let me know... I've been working with templates for a few weeks and have been able to...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as...
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as...
5
by: Brian | last post by:
I am "learning" C# and have run into a problem that, though I can work around it, I would like to know what the *right* way to handle the issue is....
3
by: Dilip | last post by:
I am stumbling my way through C++ templates and I keep running into way too many questions. Here is one: If a library writer exposes a function...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false;...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
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...
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...

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.