posting the code - further to odd problem

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Sat Dec 29 10:55:46 EST 2001


----- Original Message ----- 
From: "Dave Harrison" <dlharris at mail.usyd.edu.au>

> However the packet class code is as follows :
> 
> import string
> pdic = {}

What is the purpose of the above line?
 
> class Packet:
>         def __init__(self, info):
>                 self.buildDic(info)
> 
>         def buildDic(self, info):
>                 for item in info:
>                         split = string.split(item,"=")
>                         if (len(split) > 1):
>                                 pdic[split[0]] = split[1]
> 
>         def getVal(self, key):
>                 if pdic.has_key(key):
>                         result = pdic[key]
>                 else:
>                         result = ''
>                 return result

Here all instances use the global pdic.  This appears to be an
error to me.

Try this:

import string
 
class Packet:
        def __init__(self, info):
                self.pdic = {}
                self.buildDic(info)

        def buildDic(self, info):
                for item in info:
                        split = string.split(item,"=")
                        if (len(split) > 1):
                                self.pdic[split[0]] = split[1]

        def getVal(self, key):
                if self.pdic.has_key(key):
                        result = self.pdic[key]
                else:
                        result = ''
                return result







More information about the Python-list mailing list