469,282 Members | 1,815 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Class probkem - getting msg that self not defined

Hi Everyone,

I am having a problem with a class and hope you can help.

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)

I would have thought that self would have carried forward when I grabbed
an instance of TriggerMessage.

Any ideas on this?

The class in question is:
class TriggerMessage(object):

def __init__(self,data):
"""
Unpacks the passed binary data based on the MQTCM2 format dictated in
the MQ Application Programming Reference
"""

self.data=data
self.structid=None
self.version=None
self.qname=None
self.procname=None
self.trigdata=None
self.appltype=None
self.applid=None
self.envdata=None
self.userdata=None
self.qmgr=None
def decode(self):
import struct
format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
size=struct.calcsize(format)
self.data=data
self.structid, self.version, self.qname, self.processname, \
self.triggerdata, self.appltype, self.applid, \
self.envdata, self.userdata, self.qmgr \
= struct.unpack(format,self.data)
May 22 '06 #1
7 1693
Andrew Robert wrote:
Hi Everyone,

I am having a problem with a class and hope you can help.

When I try to use the class listed below, I get the statement that self
is not defined.

test=TriggerMessage(data)
self is not known here; only inside the class.
var = test.decode(self.qname)

I would have thought that self would have carried forward when I grabbed
an instance of TriggerMessage.

Any ideas on this?

The class in question is:
class TriggerMessage(object):

def __init__(self,data):
"""
Unpacks the passed binary data based on the MQTCM2 format dictated in
the MQ Application Programming Reference
"""

self.data=data
self.structid=None
self.version=None
self.qname=None
self.procname=None
self.trigdata=None
self.appltype=None
self.applid=None
self.envdata=None
self.userdata=None
self.qmgr=None
def decode(self):
import struct
format='4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
size=struct.calcsize(format)
self.data=data
self.structid, self.version, self.qname, self.processname, \
self.triggerdata, self.appltype, self.applid, \
self.envdata, self.userdata, self.qmgr \
= struct.unpack(format,self.data)

May 22 '06 #2
wes weston wrote:
Andrew Robert wrote:
Hi Everyone,

I am having a problem with a class and hope you can help.

When I try to use the class listed below, I get the statement that self
is not defined.

test=TriggerMessage(data)


self is not known here; only inside the class.
var = test.decode(self.qname)

<snip>

I guess I was barking up the wrong tree on that one.

How would I go about getting the required values out of the class?

Return self?
May 22 '06 #3
Andrew Robert wrote:
wes weston wrote:
Andrew Robert wrote:
Hi Everyone,

I am having a problem with a class and hope you can help.

When I try to use the class listed below, I get the statement that self
is not defined.

test=TriggerMessage(data)

self is not known here; only inside the class.
var = test.decode(self.qname)

<snip>

I guess I was barking up the wrong tree on that one.

How would I go about getting the required values out of the class?

Return self?


You can refer to the variables inside the class as test.version;
for example - as they are not protected. Or, you could add access
methods in the class like def GetVersion(self): return self.version
If you want to protect version, call it self.__version which mangles
the name from outside.
wes
May 22 '06 #4
wes weston schrieb:
Andrew Robert wrote:
wes weston wrote:
Andrew Robert wrote:

Hi Everyone,

I am having a problem with a class and hope you can help.

When I try to use the class listed below, I get the statement that self
is not defined.
test=TriggerMessage(data)

self is not known here; only inside the class.

var = test.decode(self.qname)

<snip>

I guess I was barking up the wrong tree on that one.

How would I go about getting the required values out of the class?

Return self?

You can refer to the variables inside the class as test.version;
for example - as they are not protected. Or, you could add access
methods in the class like def GetVersion(self): return self.version
If you want to protect version, call it self.__version which mangles
the name from outside.
wes


Or use the property function to define properties
<http://docs.python.org/lib/built-in-funcs.html>.

Dennis
May 22 '06 #5
Andrew Robert a écrit :
Hi Everyone,

I am having a problem with a class and hope you can help.

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)

I would have thought that self would have carried forward when I grabbed
an instance of TriggerMessage.


Hint : what is the name of your TriggerMessage instance in the above code ?

I think that what you wannt is more like this :

import struct

class TriggerMessage(object):
def __init__(self,data):
"""
Unpacks the passed binary data based on the
MQTCM2 format dictated in
the MQ Application Programming Reference
"""
self._format = '4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
self._data = data

(self.version,
self.qname,
self.processname,
self.triggerdata,
self.appltype,
self.applid,
self.envdata,
self.userdata,
self.qmgr) = self._decode()

def _decode(self):
assert len(self._data) == struct.calcsize(self._format)
return struct.unpack(self._format, self._data)

# test code
# data = ???
test=TriggerMessage(data)
var = test.qname
May 22 '06 #6

Hey Bruno,
Although I have not tested it, this appears to be it exactly.
Some confusion though.

<snip

</snip>
import struct

class TriggerMessage(object):
def __init__(self,data):
"""
Unpacks the passed binary data based on the
MQTCM2 format dictated in
the MQ Application Programming Reference
""" I am okay up to here :).

After that, well..

What does the _ before the variables mean?

Why are you defining _format and _data here? I would have thought it
belongs in the decode section.

I think it is very slick but I would like to try and understand your
approach.

Also, why assert in calculating the struct size?

Very cool how you did this.
self._format = '4s 4s 48s 48s 64s 4s 256s 128s 128s 48s'
self._data = data

(self.version,
self.qname,
self.processname,
self.triggerdata,
self.appltype,
self.applid,
self.envdata,
self.userdata,
self.qmgr) = self._decode()

def _decode(self):
assert len(self._data) == struct.calcsize(self._format)
return struct.unpack(self._format, self._data)

# test code
# data = ???
test=TriggerMessage(data)
var = test.qname

May 22 '06 #7
Andrew Robert wrote:
Hey Bruno,
Although I have not tested it, this appears to be it exactly.
Some confusion though.

<snip

</snip>
import struct

class TriggerMessage(object):
def __init__(self,data):
"""
Unpacks the passed binary data based on the
MQTCM2 format dictated in
the MQ Application Programming Reference
"""
I am okay up to here :).

After that, well..

What does the _ before the variables mean?


It's a convention for implementation attributes (not part of the class
public interface).
Why are you defining _format and _data here?
Since the data object is passed at instanciation time (at least this was
the case in your code), this is the only place where I can bind it to an
instance attribute. As for the format string, it's really an attribute
of the class, and something that won't change, so better to have it in
the __init__ too. In fact, from what I saw of your code, it may as well
be a class attribute (ie : defined outside a method, and shared by all
instances), since the same format apply for all instances.
I would have thought it
belongs in the decode section.
It is used in the _decode method, but that does not mean it must be
defined in the _decode method.
I think it is very slick but I would like to try and understand your
approach.

Also, why assert in calculating the struct size?


You don't *need* this calculation. struct.calcsize() is meant to let do
you check that the format string and the data string match (at least in
size). The assert here is mostly for developpement...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 23 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Fernando Rodriguez | last post: by
166 posts views Thread by Graham | last post: by
10 posts views Thread by David Hirschfield | last post: by
3 posts views Thread by Luis P. Mendes | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.