Hi,
i'm starting work and a simple database layer and wanted to use the __del__ to open and close a connection. However, i get an erro when executing this script:
class Table: refcount = 0 def __init__(self, name): self.name = name print "table %s " % repr(self) Table.refcount += 1 print "refcount = %s" % str(Table.refcount)
def __del__(self): Table.refcount -= 1 if (Table.refcount == 0): print "Last table standing" else: print "There is/are still %d table(s) left." % Table.refcount
def __getitem__(self,item): return "not implemented"
def howMany(self): if (Table.refcount == 1): print "Only 1 table" else: print "There are %d tables active." % Table.refcount
if __name__ == '__main__': suppliera = Table("suppliers") supplierb = Table("suppliers") print "Supplier returned from a %s: %s" % (1, suppliera[1]) print "Supplier returned from b %s: %s" % (2, supplierb[2]) suppliera.howMany() supplierb.howMany()
This produces the following output and error:
table <__main__.Table instance at 0x008D5C10>
refcount = 1
table <__main__.Table instance at 0x008D5C38>
refcount = 2
Supplier returned from a 1: not implemented
Supplier returned from b 2: not implemented
There are 2 tables active.
There are 2 tables active.
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'refcount'" in <bound method Table.__del__ of <__main__.Table instance at 0x008D5C10>> ignored
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'refcount'" in <bound method Table.__del__ of <__main__.Table instance at 0x008D5C38>> ignored
What am i doing wrong? Thanks Benedict -- http://mail.python.org/mailman/listinfo/python-list