Static Class Initialization Question.

T

Thomas Troeger

Hello,

I have a class that looks like this:

class A(object):
def __init__(self, a=0, b=1):
self.a, self.b=a, b

def __str__(self):
return "%s(%d,%d)" % (type(a).__name__, self.a, self.b)

I want to have a list of such classes instantiated automatically on
startup of my program. My current (most probably clumsy) implementation
looks like this:

bla=[A(x[0], x[1]) for x in ((1, 2), (3, 4))]

giving the following:
['A(1,2)', 'A(3,4)']

Is there a better way to construct a list of such classes? Basically
what I want is something similar to the following C example:

struct {
int a;
int b;
} bla[]={ {1, 2}, {3, 4} };

Regards,
T.
 
B

Bruno Desthuilliers

Thomas Troeger a écrit :
Hello,

I have a class that looks like this:

class A(object):
def __init__(self, a=0, b=1):
self.a, self.b=a, b

def __str__(self):
return "%s(%d,%d)" % (type(a).__name__, self.a, self.b)

Given the output example you give, I assume there's a typo here and you
meant:
return "%s(%d,%d)" % (type(self).__name__, self.a, self.b)

I want to have a list of such classes instantiated automatically on
startup of my program. My current (most probably clumsy) implementation
looks like this:

bla=[A(x[0], x[1]) for x in ((1, 2), (3, 4))]

Not clumsy at all, and almost perfectly pythonic. The only improvment I
can think of is:

bla = [A(*args) for args in ((1,2), (3,4))]

giving the following:
['A(1,2)', 'A(3,4)']

Is there a better way to construct a list of such classes?

Note that it's not a list of classes, but a list of instances of A. But
given your specs, nope, your approach is the right one.
Basically
what I want is something similar to the following C example:

struct {
int a;
int b;
} bla[]={ {1, 2}, {3, 4} };

Basically (no pun intended[1]), Python is not C. Trying to write C in
Python will only buy you pain and frustration (and this can be
generalized for any combination of two languages for any known
programming language).

[1] well... in fact, yes... !-)
 
T

Thomas Troeger

Bruno said:
return "%s(%d,%d)" % (type(self).__name__, self.a, self.b)

Er, yes exactly! I noticed it a few seconds after I had sent the message ;-(

Of course I meant class instances ... sorry :) It's always good to have
an example to compensate for English errors *g*.
bla = [A(*args) for args in ((1,2), (3,4))]
....

> Note that it's not a list of classes, but a list of instances of A. But
> given your specs, nope, your approach is the right one.

Ah I knew there was something and I couldn't find it in the docs
anymore! Now my potential follow-up question is answered as well, namely
how I can instantiate with variable argument lists, like this:
>>> bla = [A(*args) for args in ((), (1,), (1, 2))]
>>> map(str, bla)
['A(0,1)', 'A(1,1)', 'A(1,2)']
Basically (no pun intended[1]), Python is not C. Trying to write C in
Python will only buy you pain and frustration (and this can be
generalized for any combination of two languages for any known
programming language).

Hehe. I am trying to develop a program prototype in python because of
it's repaid prototyping properties, and once it's working I will port it
to C, because of speed issues and the fact that it's running on an
embedded machine without space for a python interpreter. I have like 4
Megs left, but until now noone has answered my question how I can cut
down a standard python installation so that it fit's into 4 megs.

Thanks for your quick answer :)
T.
 
M

Marc 'BlackJack' Rintsch

Of course I meant class instances ... sorry :) It's always good to have
an example to compensate for English errors *g*.

Well, "class instances" is still a little bit ambiguous in a language
where classes are objects too. ;-)
Ah I knew there was something and I couldn't find it in the docs
anymore! Now my potential follow-up question is answered as well, namely
how I can instantiate with variable argument lists, like this:
bla = [A(*args) for args in ((), (1,), (1, 2))]
map(str, bla)
['A(0,1)', 'A(1,1)', 'A(1,2)']

Looks like you want default values for the arguments of `A.__init__()`.

Ciao,
Marc 'BlackJack' Rintsch
 

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

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top