2

I am recently learning python and I am having an issue with object creation. I have created a class called pdf which helps to parse an inputted pdf(working). The issue I am having is that separate created objects are sharing memory space for a reason I am unsure of.

for root, dirnames, filenames in os.walk("../PDF_DB_100//"):
for filename in filenames:
    if filename.endswith('.pdf'):
        print filename
        pdf("../PDF_DB_100/"+filename).get_info()
        count+=1
        if count == 10:
            break

class pdf(object):
    Uno = []
    Dos = []
    Tress = []
    Quatro = []

    def __init__(self,path):
       operations, mostly appends onto the above variables
    ....

This code walks the dir for .pdf and creates an pdf object for 10 pdfs. But since the pdf object is not referenced shouldn't it go out of scope once the get_info() line is completed. Why do the separate pdf append data on to a single list?

2 Answers2

4

In Python class attributes defined on the class top level are attributes of the class itself, not instance.

Specifically what you want is probably

class pdf(object):
    def __init__(self,path):
      self.S_Linc = []
      self.Short_Legal = []
      self.Title_Number = []
      self.Legal_Description = []

       operations, mostly appends onto the above variables
    ....
Evgeny
  • 3,064
  • 2
  • 18
  • 19
  • 1
    Thank you. I need to be more throughout when reading the documentation, rather than basing framework on other languages. – user2047958 Mar 04 '13 at 17:46
1

The problem is you are declaring the list inside of the object instead of inside the constructor.

Do this instead.

class pdf(object):
    def __init__(self):
        self.Uno = []
spartacus
  • 613
  • 6
  • 12