[Tutor] Need help with using methods in a base class
Roy Khristopher Bayot
roybayot at gmail.com
Sun Sep 7 17:07:00 CEST 2008
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
... def setData(self, data):
... Parallel.setData(self, data)
... def setLatch(self, latch):
... Parallel.setDataStrobe(self, int(latch[0]))
... Parallel.setAutoFeed(self, int(latch[1]))
... Parallel.setInitOut(self, int(latch[2]))
... def generateClockPulse(self):
... Parallel.setSelect(self, 0)
... Parallel.setSelect(self, 1)
...
>>> 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.
So I did something else.
>>> class LightsHandle(Parallel):
... def __init__(self):
... Parallel.__init__(self, port = 0)
... def __del__(self):
... Parallel.__del__(self)
... def setData(self, data):
... Parallel.setData(self, data)
... def setLatch(self, latch):
... Parallel.setDataStrobe(self, int(latch[0]))
... Parallel.setAutoFeed(self, int(latch[1]))
... Parallel.setInitOut(self, int(latch[2]))
... def generateClockPulse(self):
... Parallel.setSelect(self,0)
... Parallel.setSelect(self,1)
...
>>> a = LightsHandle()
>>> a.setData(0xF0)
>>> a.setLatch('111')
>>> a.generateClockPulse()
>>> a.setLatch('110')
>>> a.generateClockPulse()
>>> a.setData(0x0F)
>>> a.setLatch('111')
>>> a.generateClockPulse()
There were no errors thrown. But the problem is that it doesnt work. This
was suppose to control 32 LEDs (8 LEDs per latch, 6 latches total). Method
setData() is suppose to control the 8 bits that you want to output on a
specific latch. Method setLatch() will specify the latch. Method
generateClockPulse() will cause the specific latch to take in the data and
retain it there.
But it doesnt work. The LEDs couldnt be controlled.
I already tried using the base class and it works just fine.
>>> from parallel import Parallel
>>> p = Parallel()
>>> p.setData(0xFF)
>>> p.setDataStrobe(1)
>>> p.setAutoFeed(1)
>>> p.setInitOut(1)
>>> p.setSelect(0)
>>> p.setSelect(1)
So I thought maybe there was something wrong with setLatch() since elements
of the string were converted to integers. I changed the code again.
>>> class LightsHandle(Parallel):
... def __init__(self):
... Parallel.__init__(self, port = 0)
... def __del__(self):
... Parallel.__del__(self)
... def setData(self, data):
... Parallel.setData(self, data)
... def setLatch(self, x, y, z):
... Parallel.setDataStrobe(self, x)
... Parallel.setAutoFeed(self, y)
... Parallel.setInitOut(self, z)
... def generateClockPulse(self):
... Parallel.setSelect(self,0)
... Parallel.setSelect(self,1)
...
>>> a = LightsHandle()
>>> a.setData(0xF0)
>>> a.setLatch(1,1,1)
>>> a.generateClockPulse()
>>> a.setData(0x0F)
>>> a.setLatch(1,1,1)
>>> a.generateClockPulse()
But no LED lights were changing as it was suppose to.
Any ideas on what I dont know or what I've overlooked?
On Sun, Sep 7, 2008 at 6:07 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:
> "Roy Khristopher Bayot" <roybayot at gmail.com> wrote
>
>
> ... def generateClockPulse(self):
>> ... parallel.Parallel.setSelect(0)
>> ... parallel.Parallel.setSelect(1)
>> ...
>>
>>> a = LightsHandle()
>>>>> a.setD(0xF0)
>>>>>
>>>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> File "<stdin>", line 5, in setD
>> TypeError: unbound method setData() must be called with Parallel instance
>> as
>> first argument (got int instance instead)
>>
>
> The error merssage gives the clue. Sonce you are calling a method
> on a classs not an object you need to pass in the instance reference
> explicitly. Its exactly as in calling the base constructor in __init__
>
> class C(P):
> def __init__(self):
> P.__init__(self) # need to pass self here
>
> (Some notes: I changed setData() to setD() so that there wont be a
>> confusion. Method setData() is from the base class Parallel. Although I
>> think setData() could be overriden.)
>>
>
> Thats not necessary, each class creates its owen namespace.
>
> What have I been doing wrong? Why does it say that I need a Parallel
>> instance?
>>
>
> Because the class needs to know where to find the instance variables.
> Since you are calling the class directly, not via an instance you have
> to pass self explicitly.
>
> Remember that in
>
> class C:
> def m(self, x): print x
>
> c = C(42)
>
>
> Then c.m() is the same as C.m(c,42). self takes on the value of
> the instance in the first case but in the second you have to pass
> it directly. When you are inside a methiod you always use the
> second form so you always need to pass self.
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080907/9cbc5ef3/attachment.htm>
More information about the Tutor
mailing list