[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Class attributes / methods lost?

Gabor Urban

3/1/2010 2:05:00 PM

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])
<>

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
--
Linux: Choice of a GNU Generation
1 Answer

Peter Otten

3/1/2010 2:25:00 PM

0

Gabor Urban wrote:

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

<snip>

> 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/...
http://www.python.org/dev/peps...

Peter