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

Kent Johnson kent37 at tds.net
Sat Sep 6 20:49:20 CEST 2008


On Sat, Sep 6, 2008 at 12:04 PM, Roy Khristopher Bayot
<roybayot at gmail.com> wrote:
> Hi. I am having some difficulty using methods from a base class.
>
> I have 2 classes. The first one is Parallel which is inside the module
> parallel. The methods useful to me from this class are setDataStrobe(),
> setAutoFeed(), setInitOut(), setSelect(), and setData().
>
> The second one is derived from the first. I called it LightsHandle. The code
> and traceback is written below:
>
>>>> import parallel
>>>> class LightsHandle(parallel.Parallel):
> ...     def __init__(self):
> ...             pass
> ...     def setData(self, data):
> ...             Parallel.setData(data)
> ...     def setLatch(self, latch):
> ...             Parallel.setDataStrobe(int(latch[0]))
> ...             Parallel.setAutoFeed(int(latch[1]))
> ...             Parallel.setInitOut(int(latch[2]))
> ...     def generateClockPulse(self):
> ...             Parallel.setSelect(0)
> ...             Parallel.setSelect(1)
> ...
>>>> a = LightsHandle()
>>>> a.setData(0xF0)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<stdin>", line 5, in setData
> NameError: global name 'Parallel' is not defined

This error is because you did not import Parallel, you imported
parallel. You have to use the full name parallel.Parallel as you
figured out. You could also use
  from parallel import Parallel
and then just refer to Parallel.

> So I tried another one. And the code and traceback is written below.
>
>>>> import parallel
>>>> class LightsHandle(parallel.Parallel):
> ...     def __init__(self):
> ...             pass
> ...     def setD(self, data):
> ...             parallel.Parallel.setData(data)
> ...     def setLatch(self, latch):
> ...             parallel.Parallel.setDataStrobe(int(latch[0]))
> ...             parallel.Parallel.setAutoFeed(int(latch[1]))
> ...             parallel.Parallel.setInitOut(int(latch[2]))
> ...     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)
>
> (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.)
>
> What have I been doing wrong? Why does it say that I need a Parallel
> instance?
>
> According to
> http://www.python.org/doc/2.5/tut/node11.html#SECTION0011340000000000000000
>
> "There is a simple way to call the base class method directly: just call
> "BaseClassName.methodname(self, arguments)". This is occasionally useful to
> clients as well. (Note that this only works if the base class is defined or
> imported directly in the global scope.) "

This quote has the answer to your question. When you call the base
class method, you have to explicitly provide the self parameter, e.g.
  parallel.Parallel.setData(self, data)

Kent


More information about the Tutor mailing list