How do I pass a list to a __init__ value/definition?

Ben Sizer kylotan at gmail.com
Tue Jul 25 08:56:48 EDT 2006


ryanshewcraft at gmail.com wrote:
> 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.

Your problem lies elsewhere, as when I do exactly that, I get the
correct results:

>>> mr = MultipleRegression(10, [1,2,3,4], 5)
>>> print mr.regressors
[1, 2, 3, 4]


So I think the way you are passing your list to MultipleRegression is
perhaps wrong. I expect your problem is in creating that list in the
first place from an unknown number of items. Basically you need to
create the list, repeatedly add the necessary items to it until you're
done, and then pass that to MultipleRegression. How to create and
populate that list will depend on where you're getting the data from.

-- 
Ben Sizer




More information about the Python-list mailing list