473,382 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1879
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rim | last post by:
Hi, It appears to me the simplest way to add a function to a class outside of the class declaration is as follows: >>> class a(object): .... pass .... >>> def f(self): .... print 'hi'
2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
2
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
8
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
10
by: David Hirschfield | last post by:
Here's a strange concept that I don't really know how to implement, but I suspect can be implemented via descriptors or metaclasses somehow: I want a class that, when instantiated, only defines...
5
by: Russell Warren | last post by:
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable...
3
by: Luis P. Mendes | last post by:
Hi, I have the following problem: I instantiate class Sistema from another class. The result is the same if I import it to interactive shell. s = Sistema("par") class Sistema:
4
by: harijay | last post by:
Hi I am new to writing module and object oriented python code. I am trying to understand namespaces and classes in python. I have the following test case given in three files runner , master and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.