pretty basic instantiation question

S

sittner

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
 
L

Leif K-Brooks

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)]
 
S

Steve Holden

Leif said:
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
 
M

meithamj

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.
 
S

Steven D'Aprano

On Mon, 23 Oct 2006 17:36:14 -0700, meithamj wrote:

[fixing top-posting]

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
 
B

Bruno Desthuilliers

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top