469,647 Members | 1,788 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Static Class Initialization Question.

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:
>>map(str, bla)
['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.
Jul 4 '08 #1
3 2051
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:
>>map(str, bla)
['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... !-)
Jul 4 '08 #2
Bruno Desthuilliers wrote:
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 ;-(
>I want to have a list of such classes instantiated automatically on
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.
Jul 4 '08 #3
On Fri, 04 Jul 2008 14:59:05 +0200, Thomas Troeger wrote:
Bruno Desthuilliers wrote:
>>I want to have a list of such classes instantiated automatically on

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
Jul 4 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Qin Chen | last post: by
4 posts views Thread by Bret Pehrson | last post: by
1 post views Thread by philwozza | last post: by
8 posts views Thread by Per Bull Holmen | last post: by
4 posts views Thread by Bram Kuijper | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.