using a tail and a buffer to get last K lines in a file
<p>I was given this buffer and told to make a reverse input that get the last K lines in a file. From what I've been trying to do, every time I tried to run the code it says that used is not an attribute of Input. Can someone please tell me why this keeps happening? Thank you in advance.</p>

<pre><code>class Input:
def __init___( self, file ):
    self.file = file # must open( &lt;filename&gt;, "rb" )
    self.length = 0
    self.used = 0
    self.buffer = ""
def read( self ):
    if self.used &lt; self.length:  # if something in buffer 
        c = self.buffer[self.used]
        self.used += 1
        return c
    else:
        self.buffer = self.file.read( 20 ) # or 2048
        self.length = len( self.buffer )
        if self.length == 0:
            return -1
        else:
            c = self.buffer[0]
            self.used = 1

     `
</code></pre>

<p>I'm going to go out on a limb here and try guessing that the problem is that you are using the wrong name for the <code>__init__</code> magic method (as noticed by <a href="https://stackoverflow.com/users/459745/hai-vu">Hai Vu</a>). Notice that there are three trailing underscores in your code instead of two.</p>

<p>Since the <code>__init__</code> method is the one called during the construction of the object to set its various attributes, the <code>used</code> attribute never gets set because the <code>__init__</code> function never gets run.</p>

<p>Afterwards, <code>used</code> is the first attribute accessed in Input.read, which makes Python complain about it being missing.</p>

<p>If I'm right, remove the underscore and it will fix the problem (though there may be others).</p>

python file buffer tail