[Tutor] problem with class - get message that self is not defined
Python
python at venix.com
Mon May 22 21:41:12 CEST 2006
(usually best to click reply-All so that the reply goes to the list)
On Mon, 2006-05-22 at 15:08 -0400, Andrew Robert wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Well.. inside the class is the decode function.
The decode method depends on "data" which is referenced as a global.
Perhaps data should be passed into decode as a second argument. My
edited version:
def decode(self, data):
^^^^^^
import struct
format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
size=struct.calcsize(format)
#self.data=data # not needed
^ ^^^^^^^^^^^^^^
self.structid, self.version, self.qname, self.processname, \
self.triggerdata, self.appltype, self.applid, \
self.envdata, self.userdata, self.qmgr \
= struct.unpack(format,data)
^^^^^ (removed self.)
>
> In that function, several variables are defined from a struct unpack.
I am suggesting that the data to be unpacked gets passed into the decode
method as an argument:
test.decode(data)
I changed the struct.unpack call to use the data argument.
Some additional suggestions:
size is never used. perhaps you want:
assert size = <correct size>
import struct
could be moved to the top of the script and imported once
Hopefully this helps.
>
> What I need is to get those values out.
>
> How to do that, I am not exactly clear.
>
>
> Andy
>
>
> Python wrote:
> > On Mon, 2006-05-22 at 14:28 -0400, Andrew Robert wrote:
> >> When I try to use the class listed below, I get the statement that
> >> self
> >> is not defined.
> >>
> >> test=TriggerMessage(data)
> >> var = test.decode(self.qname)
> > ^^^^
> > Perhaps
> > var = test.decode()
> >
> > would do what you want.
> >
> > It is not clear why you are trying to use a "qname" argument when
> > decode's only argument is self. test.decode will bind self to the test
> > object.
> >
> > Alan Gauld's web site has a useful discussion of OOP programming.
> > http://www.freenetpages.co.uk/hp/alan.gauld
> >
>
> - --
> Thank you,
> Andrew Robert
> Systems Architect
> Information Technologies
> MFS Investment Management
> Phone: 617-954-5882
>
> E-mail: arobert at mfs.com
> Linux User Number: #201204
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.3 (MingW32)
>
> iD8DBQFEcgwoDvn/4H0LjDwRAsUuAJ94rHJbBQVxgHyLYlmi1pAhJzkE1QCfR1xK
> sZlx+dHtvaZOOwRpC8tuN6o=
> =2Nsp
> -----END PGP SIGNATURE-----
--
Lloyd Kvam
Venix Corp
More information about the Tutor
mailing list