0

I have a strange error in the following code:

s = MyClass()
f = open(filename, 'r')
nbline = f.readline()
for line in iter(f):
        linesplit = line.split()
        s.add(linesplit)
f.close()

print(len(s.l))
print(nbline)

the two print don't give me the same result. Why?

the class definition is:

class MyClass:
    l = []
    def add(self, v):
        self.l.append(v)

and the file format is:

161
3277 4704 52456568 0 1340 380 425
3277 4704 52456578 1 1330 380 422
3118 4719 52456588 1 1340 390 415
3109 4732 52456598 1 1340 400 420
3182 4743 52456608 1 1350 410 427
3309 4789 52456618 1 1360 420 446
...

for this file the print are: 51020 161

and the file contain 162 line (the number of line + the line)

If I call the function one it's ok, the error appear when I call the function twice or more (it's look like previous file are read!!! :/)

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
Fractale
  • 1,503
  • 3
  • 19
  • 34
  • What does the file look like ? Cause I am guessing the first line has the number of lines and then you just want to make sure it corresponds. – Seif Sep 14 '16 at 15:04
  • first line = nb line – Fractale Sep 14 '16 at 15:10
  • after it´s: num1 num2 num3 num4 num5 num6 mum7\n – Fractale Sep 14 '16 at 15:11
  • It doesn't help to put it here in the comment like this, can you please update your question with an example file? thanks. – Seif Sep 14 '16 at 15:13
  • Possible duplicate of ["Least Astonishment" in Python: The Mutable Default Argument](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) – MSeifert Sep 14 '16 at 16:06
  • Possible duplicate of [How do I avoid having Python class data shared among instances?](http://stackoverflow.com/questions/1680528/how-do-i-avoid-having-python-class-data-shared-among-instances) – Łukasz Rogalski Sep 14 '16 at 19:45
  • I've reverted question content to match original one. Both duplicate votes should be ignored, as they refer to different question posted as edit. – Łukasz Rogalski Sep 14 '16 at 19:49

4 Answers4

1

First, thanks for the edit.

Here is a better looking and more pythonic code:

s = MyClass()
with open(filename, 'r') as f:
    nbline = f.readline()
    for line in f:
        linesplit = line.split()
        s.add(linesplit)

Then make sure you are setting self.l = [] in your MyClass

Seif
  • 1,058
  • 11
  • 19
  • sorry I didn't know I need to edit to now I will doing it :) – Fractale Sep 14 '16 at 15:38
  • This code is in a function and when i call it the second time in another file the size is different because it contain also the older file!! :/ i don't understand i close the other file and create a new object... – Fractale Sep 14 '16 at 15:48
0

Try something the lines of:

(len(list[first dimension]) + len(list[second dimension])) etc...

A bit clunky but I think it will do what you want

0

The file probably has a trailing newline or two. If len(s.l) == nbline + 1 or just print s.l[-3:] to check.

Kalpesh Dusane
  • 1,477
  • 3
  • 20
  • 27
MMN
  • 576
  • 5
  • 7
0

Ok the problem is that s.l is a class variable shared by all instances!!!

Fractale
  • 1,503
  • 3
  • 19
  • 34