469,282 Members | 1,977 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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 849
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by johny smith | last post: by
5 posts views Thread by ESOJAY | last post: by
13 posts views Thread by Winbatch | last post: by
12 posts views Thread by mlimber | last post: by
5 posts views Thread by Brian | last post: by
2 posts views Thread by Florian Loitsch | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.