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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Static Class Initialization Question.
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... !-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Static Class Initialization Question.
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Static Class Initialization Question.
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 -- http://mail.python.org/mailman/listinfo/python-list