Class probkem - getting msg that self not defined

bruno at modulix onurb at xiludom.gro
Tue May 23 04:05:22 EDT 2006


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 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list