Find the answer to your Linux question:
Results 1 to 3 of 3
Hi folks, im having some trouble printing a buffered item in python. My code is as follows: Code: Class buffer: def __init__(self, max): self.data = [0 for i in range(max)] ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    3

    [SOLVED] Python: 'Instancemethod' object is unsubscriptable - help

    Hi folks, im having some trouble printing a buffered item in python. My code is as follows:

    Code:
    Class buffer:
           def __init__(self, max):
                self.data = [0 for i in range(max)]
    
           def append(self,mag) :
               self.data.pop(0)
               self.data.append(byte)
    
           def get(self):
               return self.data
    
    ring = 4
    
    while (ring > 0):
            buf = buffer(3)
    
            for i in range(5):
                 line = ser.read(4)
                 buf.append(byte)
                 print buf.get()
    
                 ring -= 1
                 if ring == 0:
                       break
               
                  print buf.get[0]
                  print buf.get[3]

    The code is fine until I add in the last two lines: (print buf.get[0] and print buf.get[3])

    I get the error:

    "TypeError: 'instacemethod' object is unsubscriptable.

    It seems im violating pythons rules by requesting elements 0 & 3 from "buf.get"

    Might anyone be able to point me towards the direction of being able to access elements 0 or 3 from that buffer ? Or any other element for that matter.

    Much appreciated.
    - stopgo
    Last edited by stopgo; 01-19-2011 at 12:40 PM. Reason: my querry was solved.

  2. #2
    Linux Newbie tetsujin's Avatar
    Join Date
    Oct 2008
    Posts
    115
    buf.get is a member function of the object buf.

    To use buf.get, you need to call it, using buf.get() as you did in the first "print" line.

    The problem with "print buf.get[3]" is that you're treating the function as an array, where what you want is to evaluate the function and treat the result as an array.

    Basically, "print buf.get()[3]" should work.

  3. #3
    Just Joined!
    Join Date
    Dec 2010
    Posts
    3
    and it most definitely does.

    Good looking out tetsujin !

    Thanks most importantly for the explanation. Answered a few other querries I had.

    Much thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...