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

Roy Khristopher Bayot roybayot at gmail.com
Sat Sep 6 18:04:12 CEST 2008


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

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.) "

Do I have a problem in scope? How can I resolve it?

Thank you and have a nice day. ^_^
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080907/b005aaae/attachment.htm>


More information about the Tutor mailing list