How do I pass a list to a __init__ value/definition?
Simon Brunning
simon at brunningonline.net
Tue Jul 25 08:57:55 EDT 2006
On 25 Jul 2006 05:46:55 -0700, ryanshewcraft at gmail.com
<ryanshewcraft at gmail.com> wrote:
> Let me start with my disclaimer by saying I'm new to computer
> programming and have doing it for the past three weeks. I may not be
> completely correct with all the jargon, so please bear with me.
>
> Anyways, I'm writing a function which has a class called
> "MultipleRegression." I want one of the variables under the __init__
> method to be a list. I've got:
>
> class MultipleRegression:
> def __init__(self, dbh, regressors, fund):
> self.dbh = dbh
> self.regressors = regressors
>
> and I want to be able to enter regressors as a list like
> MultipleRegression(dbh, [1,2,3,4], 5). But when I do this only the 1
> gets passed to regressors and thus to self.regressors. Is there any
> simple way to fix this? Keep in mind that the length of the list may
> vary, so I can't just create a set number of variables and then mash
> them together into a list.
What you have works fine for me:
Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class MultipleRegression:
... def __init__(self, dbh, regressors, fund):
... self.dbh = dbh
... self.regressors = regressors
...
>>> spam = MultipleRegression('dbh', [1,2,3,4], 5)
>>> spam.regressors
[1, 2, 3, 4]
What makes you think you only have the first member of the list? Can
you show us the code that's not working?
--
Cheers,
Simon B,
simon at brunningonline.net,
http://www.brunningonline.net/simon/blog/
More information about the Python-list
mailing list