473,401 Members | 2,068 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,401 software developers and data experts.

Bizzare lst length problem

Ben
Hello...hopefully my last question :-)

I ave a dictionary, where each value is a class instance. I access it
using:

for k, v in self.panels.panel_list.items():
print "Number:\t",v.number
print "Level:\t",v.level
print "Location:\t",v.location
print "MOPS:\t",v.mops
print "List length:\t",len(v.mops)
print "Matrix:\t",v.matrix,"\n\n"

The output from this would be (for a given key value):
Number: 181
Level: ovride+supvis
Location: mons=4 v8.0 3rd floor
MOPS: ['287', '288', '289', '290']
List Length: 28
Matrix: kng

This is really odd...my len(v.mops) ought to return 4 (4 elements in
the list). In fact it returns 28. looking at outputs from lots of
records, it seems that the length is almost always 7 time too great
(28/7=4)....but not always. This is really confusing...can anyon
suggest what is going on?

I've been trying to output the list elements as a string with equally
limmited success, but the thing seems so simple I can't see where the
prblem might lie....

Cheers,

Ben

Oct 8 '06 #1
16 1117
Ben wrote:
The output from this would be (for a given key value):
Number: 181
Level: ovride+supvis
Location: mons=4 v8.0 3rd floor
MOPS: ['287', '288', '289', '290']
List Length: 28
Matrix: kng

This is really odd...my len(v.mops) ought to return 4 (4 elements in
the list).
adding a

print type(v.mops), repr(v.mops)

debug statement might provide you with the clues you need.
In fact it returns 28. looking at outputs from lots of
records, it seems that the length is almost always 7 time too great
(28/7=4)....but not always.
>>len("['287',")
7
>>len(" '288',")
7
>>len(" '289',")
7
>>len(" '290']")
7

</F>

Oct 8 '06 #2
Ben
Ah... my list is a string. That explains the len() results, but not why
it is a string in the dirst place.

I have a dictionary containing a number of instances of the following
class as values:

class panel:
mops =[]

def __init__(self,number,level,location,mops,matrix):
self.number=number
self.level=level
self.location=location
self.mops=mops
self.matrix=matrix
abve mops is a list, yet when I access it it is a string...

Fredrik Lundh wrote:
Ben wrote:
The output from this would be (for a given key value):
Number: 181
Level: ovride+supvis
Location: mons=4 v8.0 3rd floor
MOPS: ['287', '288', '289', '290']
List Length: 28
Matrix: kng

This is really odd...my len(v.mops) ought to return 4 (4 elements in
the list).

adding a

print type(v.mops), repr(v.mops)

debug statement might provide you with the clues you need.
In fact it returns 28. looking at outputs from lots of
records, it seems that the length is almost always 7 time too great
(28/7=4)....but not always.
>>len("['287',")
7
>>len(" '288',")
7
>>len(" '289',")
7
>>len(" '290']")
7

</F>
Oct 8 '06 #3
Ben
....and when I print out the string, it is still formatted as one would
expect a list to be:

<type 'str'"['01', '02', '03', '04']"

Ben wrote:
Ah... my list is a string. That explains the len() results, but not why
it is a string in the dirst place.

I have a dictionary containing a number of instances of the following
class as values:

class panel:
mops =[]

def __init__(self,number,level,location,mops,matrix):
self.number=number
self.level=level
self.location=location
self.mops=mops
self.matrix=matrix
abve mops is a list, yet when I access it it is a string...

Fredrik Lundh wrote:
Ben wrote:
The output from this would be (for a given key value):
Number: 181
Level: ovride+supvis
Location: mons=4 v8.0 3rd floor
MOPS: ['287', '288', '289', '290']
List Length: 28
Matrix: kng
>
This is really odd...my len(v.mops) ought to return 4 (4 elements in
the list).
adding a

print type(v.mops), repr(v.mops)

debug statement might provide you with the clues you need.
In fact it returns 28. looking at outputs from lots of
records, it seems that the length is almost always 7 time too great
(28/7=4)....but not always.
>>len("['287',")
7
>>len(" '288',")
7
>>len(" '289',")
7
>>len(" '290']")
7

</F>
Oct 8 '06 #4

Ben wrote:
Ah... my list is a string. That explains the len() results, but not why
it is a string in the dirst place.

I have a dictionary containing a number of instances of the following
class as values:

class panel:
mops =[]

def __init__(self,number,level,location,mops,matrix):
self.number=number
self.level=level
self.location=location
self.mops=mops
self.matrix=matrix
abve mops is a list, yet when I access it it is a string...
Well, if you are going to spare us from reading all of your code,
you'll have to debug it yourself. The clue that Fredrik gave you is
*not* of the use-once-and-discard variety -- when you are having
problems with the pixies changing your lists into strings, you need to
sprinkle prints of type(pixie_prey) and repr(pixie_prey) at salient
points in your code; as first statement in that __init__ method would
be a good start.

Oct 8 '06 #5

Ben wrote:
Ah... my list is a string. That explains the len() results, but not why
it is a string in the dirst place.

I have a dictionary containing a number of instances of the following
class as values:

class panel:
mops =[]

def __init__(self,number,level,location,mops,matrix):
self.number=number
self.level=level
self.location=location
self.mops=mops
self.matrix=matrix
abve mops is a list, yet when I access it it is a string...
Well, if you are going to spare us from reading all of your code,
you'll have to debug it yourself. The clue that Fredrik gave you is
*not* of the use-once-and-discard variety -- when you are having
problems with the pixies changing your lists into strings, you need to
sprinkle prints of type(pixie_prey) and repr(pixie_prey) at salient
points in your code; as first statement in that __init__ method would
be a good start.

Oct 8 '06 #6
Ben
Thanks for the advice - I'm already doing just that, so hopefully will
soon be sorted :-p
John Machin wrote:
Ben wrote:
Ah... my list is a string. That explains the len() results, but not why
it is a string in the dirst place.

I have a dictionary containing a number of instances of the following
class as values:

class panel:
mops =[]

def __init__(self,number,level,location,mops,matrix):
self.number=number
self.level=level
self.location=location
self.mops=mops
self.matrix=matrix
abve mops is a list, yet when I access it it is a string...

Well, if you are going to spare us from reading all of your code,
you'll have to debug it yourself. The clue that Fredrik gave you is
*not* of the use-once-and-discard variety -- when you are having
problems with the pixies changing your lists into strings, you need to
sprinkle prints of type(pixie_prey) and repr(pixie_prey) at salient
points in your code; as first statement in that __init__ method would
be a good start.
Oct 8 '06 #7

Ben wrote:
...and when I print out the string, it is still formatted as one would
expect a list to be:

<type 'str'"['01', '02', '03', '04']"
We know that. Fredrik deduced it and told you well over an hour ago.

Show us the code that is creating instances of the panel class ...

panel1 =
panel(number=?,level=?,location=?,mops=??????????? ?????,matrix=?)
What are you passing as the 4th positional arg
^^^^^^^^^^^^^^^^^^^^^^^ ???

Oct 8 '06 #8
Ben
Using Fredericks advice I managed to track down the problem - it was
really very stupid. I had accidentally cast the list to a string
earlier in another part of the code. Its a bit of an anticlimax really
- not mysterious at all (just mysteriously remiss on my part)

Apologies for not simple posting the entire code earlier on - but
thanks for everyone for puttin up with me, and in particular to
Frederick for his very useful hint :-)

Cheers,

Ben
John Machin wrote:
Ben wrote:
...and when I print out the string, it is still formatted as one would
expect a list to be:

<type 'str'"['01', '02', '03', '04']"

We know that. Fredrik deduced it and told you well over an hour ago.

Show us the code that is creating instances of the panel class ...

panel1 =
panel(number=?,level=?,location=?,mops=??????????? ?????,matrix=?)
What are you passing as the 4th positional arg
^^^^^^^^^^^^^^^^^^^^^^^ ???
Oct 8 '06 #9
On 8 Oct 2006 06:12:48 -0700, John Machin <sj******@lexicon.netwrote:
>
Show us the code that is creating instances of the panel class ...

panel1 =
panel(number=?,level=?,location=?,mops=??????????? ?????,matrix=?)
What are you passing as the 4th positional arg
^^^^^^^^^^^^^^^^^^^^^^^ ???
This is wholly unnecessary.

-- Theerasak
Oct 8 '06 #10

Theerasak Photha wrote:
On 8 Oct 2006 06:12:48 -0700, John Machin <sj******@lexicon.netwrote:

Show us the code that is creating instances of the panel class ...

panel1 =
panel(number=?,level=?,location=?,mops=??????????? ?????,matrix=?)
What are you passing as the 4th positional arg
^^^^^^^^^^^^^^^^^^^^^^^ ???

This is wholly unnecessary.

-- Theerasak

What is wholly unnecessary?

Oct 8 '06 #11
Ben
Ah - I found out why I had cast it to a string. I had not, at that
point, worked out ho to pass the list by value rather than reference,
and so was casting to a string as a stopgap measure that I then forgot
about. Now the problem is fixed after this group told me how to pass a
list by value (by slicing the entire list)
John Machin wrote:
Theerasak Photha wrote:
On 8 Oct 2006 06:12:48 -0700, John Machin <sj******@lexicon.netwrote:
>
Show us the code that is creating instances of the panel class ...
>
panel1 =
panel(number=?,level=?,location=?,mops=??????????? ?????,matrix=?)
What are you passing as the 4th positional arg
^^^^^^^^^^^^^^^^^^^^^^^ ???
This is wholly unnecessary.

-- Theerasak


What is wholly unnecessary?
Oct 8 '06 #12
Ben wrote:
Ah - I found out why I had cast it to a string. I had not, at that
point, worked out ho to pass the list by value rather than reference,
and so was casting to a string as a stopgap measure that I then forgot
about. Now the problem is fixed after this group told me how to pass a
list by value (by slicing the entire list)
if you write code that needs to treat a list as a distinct mutable
value, make sure *your* code makes a copy. relying on the caller to
remember to do that in all cases is way too error prone.

in other words, instead of doing

def function(seq):
# modify the sequence

...

# must pass in a copy, or things will break in mysterious ways
function(list(mylist))

do

def function(seq):
seq = list(seq) # make a distinct copy
# modify the sequence

...

function(seq)

</F>

Oct 8 '06 #13

Ben wrote:
Ah - I found out why I had cast it to a string. I had not, at that
point, worked out ho to pass the list by value rather than reference,
and so was casting to a string as a stopgap measure that I then forgot
about. Now the problem is fixed after this group told me how to pass a
list by value (by slicing the entire list)
All argument passing is by reference. What you are calling "pass a list
by value" is actually two steps:

(1) make a copy of a list
(2) pass a reference to the copy

At the C implementation level, it's a (PyObject *) -- the address of
the object.

HTH,
John

Oct 8 '06 #14
Ben wrote:
Ah... my list is a string. That explains the len() results, but not why
it is a string in the dirst place.

I have a dictionary containing a number of instances of the following
class as values:

class panel:
mops =[]
This one is a class attribute - it's shared between all instances of panel.
def __init__(self,number,level,location,mops,matrix):
self.number=number
self.level=level
self.location=location
self.mops=mops
And here, you create an instance attribute with the same name, that will
shadow the class attribute.
self.matrix=matrix
abve mops is a list, yet when I access it it is a string...
the class attribute 'mops' is a list. The instance attribute 'mops' is
whatever you passed when instanciating panel.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 9 '06 #15
Ben wrote:
Using Fredericks advice I managed to track down the problem - it was
really very stupid. I had accidentally cast the list to a string
There's nothing like "type casting" in Python. You did not "cast the
list to a string", you created a string from a list.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 9 '06 #16
Ben wrote:
(OT : Ben, please stop top-posting, it's really annoying)0
Ah - I found out why I had cast it to a string.
cf my previous anwser on this point.
I had not, at that
point, worked out ho to pass the list by value rather than reference,
There's nothing like 'pass by value/pass by reference' in Python. When
passing arguments to a function, the arguments *names* are local to the
function, and the arguments "values" are really the objects you passed
to the function (not copies of these objects).
and so was casting to a string as a stopgap measure that I then forgot
about. Now the problem is fixed after this group told me how to pass a
list by value (by slicing the entire list)
It's still not "passing by value". It's just passing a *copy* of the
original list.

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

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

Similar topics

1
by: nick | last post by:
In the web site http://greywolfdesign.com , there is a pop-up menu (when mouse over 'Portfolio' menu item), it always works well when using IE (6). However, when using Mozilla 1.6, the popup...
18
by: Richard Gutery | last post by:
I have an ASP page that I need to have a Message Box on. When I try to run the page, I get the following message : Exception Not Handled. Permission Denied. To trouble shoot, I gave everyone...
8
by: BCC | last post by:
I am getting a non-reproduceable release mode only error, that is proving to be really a pain. I can in some cases narrow it down by try/catch blocks but even that behavior is totally f*ed up. ...
1
by: Zri Man | last post by:
I have come across a bizzare behaviour with DB2/UDB 8.2 on SuSE Linux 2.41 When I have a MQT Refresh going on (complete refresh) it appears to lock the underlying base tables used to build the...
0
by: Josef.Szeliga | last post by:
I have a most unusual problem that i cannot work out. I have a pie chart on my report which when you click on one of the pieces calls itself and drills down into the data. This method works...
7
by: Martin Pritchard | last post by:
Hi, Sorry for my ignorance, but I'm a bit new to C++. I've been handed over a C++ app written in VS2002 which I have to convert to VS2005. Apparently it's been written in a C style, but cannot...
2
by: the.duckman | last post by:
G'Day, I have a simple peice of code behaving in a rather eratic manner. outputStreams = new MemoryStream(); //.... some code to add values to the stream ... bool ready = false; //.... some...
3
by: PT | last post by:
Hi, I've a simple XML tree that is essentially. <?xml version="1.0"?> <Project> <Version Version="1.001" /> <Platform Name="Platform1"/> <Platform Name="Platform2" /> </Project>
7
by: Okonita | last post by:
Hello Gurus, At about 11:00 am this morning, tried to connect to an instance of DB2 UDB v8. 2 fixpak 14 on a Red Hat Linux environment. The message returned say DB2 alias not found. Alarmed, we...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.