0
class A:
   id = ""
   fld = []
   def __init__(self, id):
      self.id = id
                                                                                                                                                                               
                                                                                                                                                                               
def method(inp):
   a = A("a")
   b = A("b")
   c = A("c")
   a.fld.append("Histogram")
   a.fld.append("Dendogram")
   b.fld.append("Milligram")
   b.fld.append("Pictogram")
   c.fld.append("Pro--gram")
   c.fld.append("Dodge-ram")
   print a.fld
   print b.fld
   print c.fld
   inp.append(a)
   inp.append(c)
   inp.append(b)
                                                                                                                                                                               
#__main__                                                                                                                                                          
inp = []
method(inp)
                                                                                                                                                                               
for ins in inp:
    print "-----> " + ins.id
    for fl in ins.fld:
        print fl

Trying to understand why 18.print prints all the 12_17.appends
I am completely new and practising. Pls help!

['Histogram', 'Dendogram', 'Milligram', 'Pictogram', 'Pro--gram', 'Dodge-ram']
['Histogram', 'Dendogram', 'Milligram', 'Pictogram', 'Pro--gram', 'Dodge-ram']
['Histogram', 'Dendogram', 'Milligram', 'Pictogram', 'Pro--gram', 'Dodge-ram']
-----> a
Histogram
Dendogram
Milligram
Pictogram
Pro--gram
Dodge-ram
-----> c
Histogram
Dendogram
Milligram
Pictogram
Pro--gram
Dodge-ram
-----> b
Histogram
Dendogram
Milligram
Pictogram
Pro--gram
Dodge-ram
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by vegaseat
0

The way your class is written the list 'fld' is global to all instances of the class. You need to write your class this way ...

class A:
   id = ""
   #fld = []
   def __init__(self, id):
       self.fld = []
       self.id = id
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.