-1

I have a class called "data-packet" shown below. I am trying to instantiate it so that I can use the values returned by the methods getAcc and getRot. I have also included the code that is creating an instance of the data_packet class in "main" I am getting this error:

Traceback (most recent call last)
mydata = data_packet()
TypeError: __init__() takes exactly 2 arguments (1 given)

My assumption is that the instance is passing mydata to the argument called self, but nothing is being passed to l. If I am correct, how can I solve this error?

class data_packet (object):
    def __init__(self, l):

        self.data = [0,0,0,0,0,0,0,0,0]
        self.type = ord(l[2])

        if self.type == 0:   # accel

            self.data[0] = four_bytes(l[3],l[4],l[5],l[6]) * 1.0 / (1<<16)
            self.data[1] = four_bytes(l[7],l[8],l[9],l[10]) * 1.0 / (1<<16)
            self.data[2] = four_bytes(l[11],l[12],l[13],l[14]) * 1.0 /(1<<16)

        elif self.type == 6:   # heading
            self.data[0] = four_bytes(l[3],l[4],l[5],l[6]) * 1.0 / (1<<16)

        else:   # unsupported
            pass


    def display(self):    
        if self.type == 0:
            print 'accel: %7.3f %7.3f %7.3f' % \
                     (self.data[0], self.data[1], self.data[2])

        elif self.type == 6:
            print 'heading: %7.4f' % self.data[0]

        else:
            print 'what?'

    def getAcc(self):
        while self.type == 0:

            .....dosomethings....

        return accelData

    def getRot(self):
        while self.type == 5:

        ..... dosomethings....

        return rotData


def main():
    mydata = data_packet()
    mydata.getAcc()
    mydata.getRot()
    print "********* : ", mydata.getAcc()


if __name__ == "__main__": 
    main()
7
  • 3
    You say the code is below, but you didnt actually put any of the code. That would help us a lot Commented Dec 16, 2015 at 13:40
  • My apologies... I have been struggling to post the code. Check the edited version of the question. Thanks Commented Dec 16, 2015 at 13:47
  • 2
    Er, you solve this by passing the value of l in the instantiation. Why is that confusing? Commented Dec 16, 2015 at 13:48
  • And l being an iterable. Commented Dec 16, 2015 at 13:49
  • @a_bhi_9 : actually it has to be subscriptable, not iterable... Commented Dec 16, 2015 at 13:53

2 Answers 2

1

The self in object methods is a reference to the instantiated object. When calling a method via dot notation, e.g. mydata.getAcc(), the object is automatically passed on as the first parameter. A more explicit way of achieving the same is calling the method via the class: data_package.getAcc(mydata).

__init__ is called automatically at object instantiation, but otherwise it works the same. Writing data_package() can be imagined as data_package.__init(new_object). However, in your class definition, you defined a second parameter l for __init. Your object instantiation should be data_package(l), that way, l is passed over to __init__ as its second parameter. i.e. data_package.__init(new_object, l).

Sign up to request clarification or add additional context in comments.

11 Comments

When I pass "l" as the argument as follows mydata = data_packet(l), I get this NameError: global name 'l' is not defined
Thank you all for the feedback
You have to define l before passing it to your class instantiation. Most probably l is a list...
Any suggestions on how I can do that given the above code??
well, where is the data coming from that you want to put into four_bytes()?
|
1

The answer seems pretty obvious from both the code and the traceback: your data_packet class takes a required argument l (that is "L" lower-case - an identifier one should never use...) - obviously a list FWIW - that you don't pass.

NB:self is the current instance and is automagically passed in.

2 Comments

When I pass "l" as the argument as follows mydata = data_packet(l), I get this NameError: global name 'l' is not defined
l is not defined because you have probably not initialized it before creating the data_packet object. also you do not have to name it l when you pass it to your object i.e mydata = data_packet([1,2,3,4,5,6,7,8,9,....])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.