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

R

ryanshewcraft

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.

Thanks so much! I really am getting into this whole programming thing.
Its real challenging and very useful for my work.
 
A

Andrew Koenig

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.

Really?

class MultipleRegression:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = regressors
foo = MultipleRegression(42, [1,2,3,4], 5)
print foo.regressors

prints [1,2,3,4]

Try it and see.
 
B

Ben Sizer

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.
 
S

Simon Brunning

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..... 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?
 
R

ryanshewcraft

You guys are right it is getting passed initially. I checked in by
printing the self.regressors in the class and it gives the list. But I
get the error: TypeError: iteration over non-sequence for

def Regress(self):
print self.regressors
for reg in self.regressors:
index = HedgeFund(self.dbh, reg)
indexTS[reg] = FundReturnSeries(dbh,index, sd, ed)
indexNames[reg] = index.Name()
header.append(index.Name())
header.append("t-statistic")
header.append("r-squared")
fh_csv.writerow(header)

and when I print self.regressors here it only gives me the first number
in the list. This bit of code is directly below the __init__ method.
Any ideas what I'm doing wrong? Thanks for the quick response so far.
 
B

Bruno Desthuilliers

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

<ot>
While legal (in Python) and sometimes handy, it's somewhat uncommon to
define classes in functions. Perhaps a problem with jargon ?-)
called
"MultipleRegression." I want one of the variables under the __init__
method to be a list.

Then pass in 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.

Using your code (copy-pasted for the class definition), I get this result:
m = MultipleRegression('dbh', [1,2,3,4], 5)
m.regressors [1, 2, 3, 4]

Looks like you didn't send the real minimal code sample exposing the
problem.

FWIW, one possible cause would be misunderstanding of the concept of
'reference', ie :
regressors = [1, 2, 3, 4]
m1 = MultipleRegression('dbh', regressors, 5)
m1.regressors [1, 2, 3, 4]
regressors.append(42)
m1.regressors [1, 2, 3, 4, 42]

If this happens to be your real problem, you can solve it by storing a
*copy* of the regressors list. Depending on what you really store in
'regressors', you'll need a simple copy or a deep copy:

1/ simple copy, suitable if regressors list items are immutable
(numerics, strings, tuples, ...) or if it's ok to have references to
(not copies of) these items:

class MultipleRegression:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = regressors[:] # makes a copy of the list

2/ deep copy, in case you need it (but read the Fine Manual before...):

import copy

class MultipleRegression:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = copy.deepcopy(regressors)


I really am getting into this whole programming thing.
Its real challenging and very useful for my work.

Welcome onboard !-)
 
B

Bruno Desthuilliers

You guys are right it is getting passed initially. I checked in by
printing the self.regressors in the class and it gives the list. But I
get the error: TypeError: iteration over non-sequence for

def Regress(self):
<ot>
the usual Python coding style is to use either all_lowercase
(preferably) or mixedCase names for functions/methods names
print self.regressors
for reg in self.regressors:
index = HedgeFund(self.dbh, reg)
indexTS[reg] = FundReturnSeries(dbh,index, sd, ed)

Names 'sd' and 'ed' are undefined.
indexNames[reg] = index.Name()
header.append(index.Name())
header.append("t-statistic")
header.append("r-squared")
fh_csv.writerow(header)

and when I print self.regressors here it only gives me the first number
in the list. This bit of code is directly below the __init__ method.
Any ideas what I'm doing wrong?

Sorry, I lack the needed psychic powers to find a bug in a code I can't
see !-)

Some wild guesses:
* you overwrite self.regressors somewhere else
* there's at least one case where you instanciate MultipleRegression
with someting that is not iterable.


FWIW, always try to reduce code to the minimal runnable snippet
exhibiting the problem, so others have a chance to help you. As a
side-effect, one very often finds the problem while doing so !-)


HTH
 
R

ryanshewcraft

Bruno may be right. At some point I've got

del self.regressors[fundNumber]

which eliminates one of the variables in the list. I guess I figured
it would be alright because I thought the program would run in a linear
fashion (aside from loops, etc). I use the list in the code above
where I delete the variable. Its weird because the deletion is under
an "if" that only occurs if the Regress method runs correctly. Somehow
it seems to anticipate the deletion of the variable before it occurs.
Its a bit like quantum mechanics?
 
R

ryanshewcraft

FWIW, always try to reduce code to the minimal runnable snippet
exhibiting the problem, so others have a chance to help you. As a
side-effect, one very often finds the problem while doing so !-)

Sorry I can't post to much of the code. Some of what I'm using is
grabbing infromation off of our internal database, and for compliance
reasons I can't print enough code so that it would actually run.
Thanks for you help anyways, I'll try and figure out what I can. If
necessary I'll retool the whole thing
 
B

Bruno Desthuilliers

Sorry I can't post to much of the code. Some of what I'm using is
grabbing infromation off of our internal database, and for compliance
reasons I can't print enough code so that it would actually run.

"Reducing to the minimal runnable snippet exhibiting the problem"
actually implies not depending on any DB or whatever. Nothing prevents
you from replacing these parts with mock objects returning dummy data.
And this is exactly why this process very often reveals the problem...
 
B

Bruno Desthuilliers

Bruno may be right. At some point I've got

del self.regressors[fundNumber]

oops... You're probably in for troubles with this:
l = [1, 2, 3, 4]
del l[1]
l [1, 3, 4]
del l[1]
l [1, 4]
del l[1]
l [1]
del l[1]
Traceback (most recent call last):

You perhaps want a dict instead:
l = [1, 2, 3, 4]
d = dict(zip(l, l))
d {1: 1, 2: 2, 3: 3, 4: 4}
del l[1]
l [1, 3, 4]
d {1: 1, 2: 2, 3: 3, 4: 4}
del d[1]
d {2: 2, 3: 3, 4: 4}
del d[1]
Traceback (most recent call last):
File said:
which eliminates one of the variables in the list.

This *won't* turn the list into an integer:
.... del l[0]
....
l []
for item in l: print item ....
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top