473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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.

Jul 25 '06 #1
10 1663
<ry***********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
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.
Jul 25 '06 #2
ry***********@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

Jul 25 '06 #3
On 25 Jul 2006 05:46:55 -0700, ry***********@gmail.com
<ry***********@gmail.comwrote:
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,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 25 '06 #4
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.

Jul 25 '06 #5
ry***********@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
<ot>
While legal (in Python) and sometimes handy, it's somewhat uncommon to
define classes in functions. Perhaps a problem with jargon ?-)
</ot>
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 !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 25 '06 #6
ry***********@gmail.com wrote:
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
</ot>
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
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 25 '06 #7
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?

Jul 25 '06 #8

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

Jul 25 '06 #9
ry***********@gmail.com wrote:
>
>>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.
"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...
Thanks for you help anyways, I'll try and figure out what I can. If
necessary I'll retool the whole thing

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 25 '06 #10
ry***********@gmail.com wrote:
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):
File "<stdin>", line 1, in ?
IndexError: list assignment index out of range
>>>
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 "<stdin>", line 1, in ?
KeyError: 1
>>del d[4]
d
{2: 2, 3: 3}
>>>
which eliminates one of the variables in the list.
This *won't* turn the list into an integer:
>>l = [1, 2, 3, 4]
while l:
.... del l[0]
....
>>l
[]
>>for item in l: print item
....
>>>


--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 25 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: meinrad recheis | last post by:
hi pythoneers i am very annoyed by the List's index out of bouds exception it forces me to complicate my code by adding length checking i might be spoilt by Ruby which returns nil for not...
3
by: user | last post by:
I have gotten properties to respond correctly, but when I try to do it in __init__: class foo: def getter(self): return "hello" def __init__(self):
3
by: Zunbeltz Izaola | last post by:
Hi, I'm playing with __slot__ (new class) and the __init__ method of my class. I want the arguments of __init__ to be keywords and that the method initialize all the attributes in __slot__ to...
4
by: GrelEns | last post by:
hello, i wonder if this possible to subclass a list or a tuple and add more attributes ? also does someone have a link to how well define is own iterable object ? what i was expecting was...
2
by: Aaron | last post by:
I have a data sructure setup and I populate it in a loop like so: y=0 while X: DS.name = "ASDF" DS.ID = 1234 list = DS; y = y + 1
5
by: Gerard Flanagan | last post by:
Hello all Could anyone shed any light on the following Exception? The code which caused it is below. Uncommenting the 'super' call in 'XmlNode' gives the same error. If I make XmlNode a...
6
by: ahart | last post by:
I'm pretty new to python and am trying to write a fairly small application to learn more about the language. I'm noticing some unexpected behavior in using lists in some classes to hold child...
40
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python...
25
by: Erik Lind | last post by:
I'm new to Python, and OOP. I've read most of Mark Lutz's book and more online and can write simple modules, but I still don't get when __init__ needs to be used as opposed to creating a class...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.