473,663 Members | 2,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
"MultipleRegres sion." I want one of the variables under the __init__
method to be a list. I've got:

class MultipleRegress ion:
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
MultipleRegress ion(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 1676
<ry***********@ gmail.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
class MultipleRegress ion:
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
MultipleRegress ion(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 MultipleRegress ion:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = regressors
foo = MultipleRegress ion(42, [1,2,3,4], 5)
print foo.regressors

prints [1,2,3,4]

Try it and see.
Jul 25 '06 #2
ry***********@g mail.com wrote:
I've got:

class MultipleRegress ion:
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
MultipleRegress ion(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 = MultipleRegress ion(10, [1,2,3,4], 5)
print mr.regressors
[1, 2, 3, 4]
So I think the way you are passing your list to MultipleRegress ion 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 MultipleRegress ion. 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***********@g mail.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
"MultipleRegres sion." I want one of the variables under the __init__
method to be a list. I've got:

class MultipleRegress ion:
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
MultipleRegress ion(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 MultipleRegress ion:
.... def __init__(self, dbh, regressors, fund):
.... self.dbh = dbh
.... self.regressors = regressors
....
>>spam = MultipleRegress ion('dbh', [1,2,3,4], 5)
spam.regresso rs
[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***@brunningo nline.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] = FundReturnSerie s(dbh,index, sd, ed)
indexNames[reg] = index.Name()
header.append(i ndex.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***********@g mail.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
"MultipleRegres sion." I want one of the variables under the __init__
method to be a list.
Then pass in a list.
I've got:

class MultipleRegress ion:
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
MultipleRegress ion(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 = MultipleRegress ion('dbh', [1,2,3,4], 5)
m.regressor s
[1, 2, 3, 4]
>>>
Looks like you didn't send the real minimal code sample exposing the
problem.

FWIW, one possible cause would be misunderstandin g of the concept of
'reference', ie :
>>regressors = [1, 2, 3, 4]
m1 = MultipleRegress ion('dbh', regressors, 5)
m1.regresso rs
[1, 2, 3, 4]
>>regressors.ap pend(42)
m1.regresso rs
[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 MultipleRegress ion:
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 MultipleRegress ion:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = copy.deepcopy(r egressors)
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***********@g mail.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] = FundReturnSerie s(dbh,index, sd, ed)
Names 'sd' and 'ed' are undefined.
indexNames[reg] = index.Name()
header.append(i ndex.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 MultipleRegress ion
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***********@g mail.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

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

Similar topics

5
1841
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 existing indices. i want to change the List so that it returns None if the index for accesssing list elements is out of bound.
3
1992
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
1700
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 None except those in the argument of __init__ (Is this a good practice?). I've the following class class PersonalData(object):
4
1847
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 something like : >>> t = Test('anAttributeValue', ) >>> t.anAttribute
2
2148
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
1655
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 subclass of 'object' rather than 'list' then the code will run. Thanks in advance. Exception:
6
1670
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 objects. Here is some abbreviated code to help me explain. #################################### class Item(object) __text = "" def __get_text(self): return self.__text
40
2712
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 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) on win32 Type "help", "copyright", "credits" or "license" for more
25
2497
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 instance by assignment. For some strange reason the literature seems to take this for granted. I'd appreciate any pointers or links that can help clarify this. Thanks
0
8437
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8861
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.