Class attributes / methods lost?

G

Gabor Urban

Hi guys,

I am building a nested data structure with the following compontens:

<>

class Item:
def __init__(self, pId, pChange, pComment):
self.ID = pId
self.Delta = pChange
self.Comment = pComment

def PrintItem(self):
str = '%s,%s,%s'%(self.ID,self.Delta,self.comment)
return str

class Package:
def __init__(self, pSerial, pDate, pOwner, pSchema):
self.serial = pSerial
self.date = pDate
self.owner = pOwner
self.schema = pSchema
self.items = []

def insertItem(self, pItem):
self.items.append(pItem)

def printPackage(self):
lines = []
str = '%s,%s,%s,%s'%(self.serial,self.date,self.owner,self.schema)
number = self.items.length
for idx in xrange(number):
line = ''
istr = self.items[idx].PrintItem()
line = str + ',' + istr
lines.append(line)
return lines

<>

The above structure is processed with next code:

<>

size = len(packages)
package = None
for index in xrange(size):
logger.info('PrettyPrinting package id=%d',index)
oplines = []
oplines = packages[index].printPackage()
for i in xrange(oplines.length):
data.append(oplines)
<>

I've got the next error message:
' Traceback (most recent call last):
File "XmlHistory_orig.py", line 203, in ?
oplines = packages[index].printPackage()
TypeError: unbound method printPackage() must be called with Package
instance as first argument (got nothing instead) '

I did give a try to access the fields directly

<>
packages = []
data = []

size = len(packages)
for index in xrange(size):
str = '%s,%s,%s,%s'%(packages[index].serial,packages[index].date,packages[index].owner,packages[index].schema)
itemnum = len(packages[index].items)
oplines = []
for itemIndx in xrange(itemnum):
element =
'%s,%s,%s'%(packages[index].items[itemIdx].ID,packages[index].items[itemIdx].Delta,packages[index].items[itemIdx].comment)
oplines.append(str + ','+elemt)

<>

Error:

Traceback (most recent call last):
File "XmlHistory.py", line 204, in ?
str = '%s,%s,%s,%s'%(packages[index].serial,packages[index].date,packages[index].owner,packages[index].schema)
AttributeError: class Package has no attribute 'serial'

The strange in this issue for me, that I do not see why are the class
methods and fields lost.

Any idea is wellcome.

Gabor
 
P

Peter Otten

Gabor said:
I am building a nested data structure with the following compontens:

Any idea is wellcome.

The error messages suggest that you are using classes where you should be
using class instances, but you don't provide the code where this problem
originates.

The code you do give looks like a very literal and incomplete conversion
from Java. Rather than go into the details I suggest that you have a look at
the Tutorial (and PEP 8) before you proceed.

http://docs.python.org/tutorial/index.html
http://www.python.org/dev/peps/pep-0008/

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top