440,772 Members | 937 Online
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

# Bizzare lst length problem

 P: n/a 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 Replies

 P: n/a 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 Oct 8 '06 #2

 P: n/a 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 Oct 8 '06 #3

 P: n/a ....and when I print out the string, it is still formatted as one would expect a list to be: 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 Oct 8 '06 #4

 P: n/a 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

 P: n/a 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

 P: n/a 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

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

 P: n/a 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:

 P: n/a On 8 Oct 2006 06:12:48 -0700, John Machin 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

 P: n/a Theerasak Photha wrote: On 8 Oct 2006 06:12:48 -0700, John Machin

 P: n/a 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 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

 P: n/a 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) Oct 8 '06 #13

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion.