Python

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • problem with class - get message that self is notdefined

    5 answers - 1568 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    PGP SIGNED MESSAGE
    Hash: SHA1
    Hi Everyone,
    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)
    - --
    Thank you,
    Andrew Robert
    Systems Architect
    Information Technologies
    MFS Investment Management
    Phone: 617-954-5882
    E-mail: arobert (AT) mfs (DOT) com
    Linux User Number: #201204
    PGP SIGNATURE
    Version: GnuPG v1.4.3 (MingW32)
    0KteYe8olY5/72+uktGRCco=
    =ToBq
    PGP SIGNATURE
    Tutor maillist - Tutor (AT) python (DOT) org
  • No.1 | | 529 bytes | |

    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 P programming.
  • No.2 | | 2335 bytes | |

    (usually best to click reply-All so that the reply goes to the list)

    Mon, 2006-05-22 at 15:08 -0400, Andrew Robert wrote:
    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:
    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 P programming.

    - --
    Thank you,
    Andrew Robert
    Systems Architect
    Information Technologies
    MFS Investment Management
    Phone: 617-954-5882

    E-mail: arobert (AT) mfs (DOT) com
    Linux User Number: #201204
    PGP SIGNATURE
    Version: GnuPG v1.4.3 (MingW32)

    sZlx+dHtvaZRpC8tuN6o=
    =2Nsp
    PGP SIGNATURE
  • No.3 | | 1579 bytes | |

    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.

    self is a special varianble that is only defined inside of a
    method of an object. It refers to the object itself (hence the name!)
    Thus you cannot pass self.qname into a method unless you are
    already inside another object's method.

    You can read about this in more edetail in the P topic
    of my tutor but I'll try to illustrate here:

    class C:
    def method(self,x):
    self.var = x
    def g(self, ):
    (self.var)

    class D:
    def f(self, val):
    print val

    c = C()
    c.method(42)

    creates an object called c and executes its method with a
    value of 42. This executes the function

    C.method(c,42)

    where self takes the values c and x 42
    The end result is that object c has a data member
    called var with the value 42

    d = D()
    c.g(d)

    creates a new object d
    calls the g method of c passing object d as argument
    executes the function

    C.g(c,d)

    where self takes on the value c and takes on d
    C.g() then calls d.f(self.var) which executes

    D.f(d,c.var)

    that is, the self in D.f() is d and the self in C.g() = c

    I hope that makes sense.

    Alan Gauld
    Author of the Learn to Program web site

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.4 | | 814 bytes | |

    >self is a special varianble
    "special variable" could be confusing. Python has no "special
    variables". "self" is used by convention.

    Good point. The name self is not special it is its role that is
    special.

    thus

    class C:
    def m(s,p):
    print p

    c = C()
    c.m(42)

    Thus we define method m to have two parameters s and p
    but when we call m we only pass in one argument. Python
    then takes this and turns it into

    C.m(c,42)

    The specialness of 'self' is that although we specify it, and can
    access it,
    in the method definition we do not specify it in the message to the
    class
    that invokes the method.

    Alan Gauld
    Author of the Learn to Program web site

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.5 | | 842 bytes | |

    Alan Gauld 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)
    >>

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

    self is a special varianble
    "special variable" could be confusing. Python has no "special
    variables". "self" is used by convention. could use "this" as an
    alternative, or any other variable. When defining a method in a class
    one writes def abc(self, a,b,c) R def abc(this, a,b,c). Then one refers
    to "self" or "this" within the method to refer to the instance calling
    the method.

Re: problem with class - get message that self is notdefined


max 4000 letters.
Your nickname that display:
In order to stop the spam: 6 + 5 =
QUESTION ON "Python"

EMSDN.COM