Bizzare lst length problem

B

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
 
F

Fredrik Lundh

Ben said:
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>
 
B

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 said:
Ben said:
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>
 
B

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']"


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 said:
Ben said:
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>
 
J

John Machin

Ben said:
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.
 
J

John Machin

Ben said:
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.
 
B

Ben

Thanks for the advice - I'm already doing just that, so hopefully will
soon be sorted :p


John said:
Ben said:
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.
 
J

John Machin

Ben said:
...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
^^^^^^^^^^^^^^^^^^^^^^^ ???
 
B

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 said:
Ben said:
...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
^^^^^^^^^^^^^^^^^^^^^^^ ???
 
T

Theerasak Photha

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
 
B

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)
 
F

Fredrik Lundh

Ben said:
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>
 
J

John Machin

Ben said:
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
 
B

Bruno Desthuilliers

Ben said:
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.
 
B

Bruno Desthuilliers

Ben said:
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.
 
B

Bruno Desthuilliers

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.
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top