[Tutor] Need help with using methods in a base class

Kent Johnson kent37 at tds.net
Sun Sep 7 20:35:22 CEST 2008


On Sun, Sep 7, 2008 at 11:07 AM, Roy Khristopher Bayot
<roybayot at gmail.com> wrote:
> Hi. I added self to parts of the code. But after making an instance and
> using the setData method it gave out an AttributeError.
>
>>>> from parallel import Parallel
>>>> class LightsHandle(Parallel):
> ...     def __init__(self):
> ...             pass

This will *prevent* Parallel.__init__() from being called. I guess
this is not what you want, it is probably the cause of your trouble.
Is _fd initialized in Parallel.__init__() ?

> ...     def setData(self, data):
> ...             Parallel.setData(self, data)

This method is not needed at all. If you omit it, the base class
method will be called automatically when you call setData() on a
LightsHandle instance.

> ...     def setLatch(self, latch):
> ...             Parallel.setDataStrobe(self, int(latch[0]))
> ...             Parallel.setAutoFeed(self, int(latch[1]))
> ...             Parallel.setInitOut(self, int(latch[2]))

This could be written more simply and idiomatically as

...     def setLatch(self, x, y, z):
...             self.setDataStrobe(x)
...             self.setAutoFeed(y)
...             self.setInitOut(z)

Since you have not overridden these methods you can call them directly.

> ...     def generateClockPulse(self):
> ...             Parallel.setSelect(self, 0)
> ...             Parallel.setSelect(self, 1)

Same here.

>>>> a = LightsHandle()
>>>> a.setData(0xF0)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<stdin>", line 5, in setData
>   File "/usr/lib/python2.5/site-packages/parallel/parallelppdev.py", line
> 563, in setData
>     return self.PPWDATA(d)
>   File "/usr/lib/python2.5/site-packages/parallel/parallelppdev.py", line
> 465, in PPWDATA
>     fcntl.ioctl(self._fd, PPWDATA,struct.pack('B',byte))
> AttributeError: LightsHandle instance has no attribute '_fd'
>
> Does this mean I have to make '_fd' in class LightsHandle? I thought that it
> would be somewhat "messy". And there might be other variables that werent
> accounted for.

Probably it means you have to call the base class __init__().

>>>> a = LightsHandle()
>>>> a.setData(0xF0)
>
> There were no errors thrown. But the problem is that it doesnt work.

> I already tried using the base class and it works just fine.
>
>>>> from parallel import Parallel
>>>> p = Parallel()
>>>> p.setData(0xFF)

Note this is a different value than you used above, is that significant?

Kent


More information about the Tutor mailing list