Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38944
| Date | 2013-02-15 19:28 +0100 |
|---|---|
| From | Bob Brusa <bob.brusa@gmail.com> |
| Subject | Re: inheritance and how to use it |
| References | <511E6971.5040109@gmail.com> <511E6C25.9020502@davea.name> <511E6F05.1090201@gmail.com> <511E7885.8060900@davea.name> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1833.1360952932.2939.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
Am 15.02.2013 19:03, schrieb Dave Angel:
> On 02/15/2013 12:23 PM, Bob Brusa wrote:
>> Am 15.02.2013 18:11, schrieb Dave Angel:
>>> On 02/15/2013 11:59 AM, Bob Brusa wrote:
>>>> Hi,
>>>> I use a module downloaded from the net. Now I want to build my own
>>>> class, based
>>>> on the class SerialInstrument offered in this module - and in my class
>>>> I would
>>>> like to initialize a few things, using e. g. the method clear()
>>>> offered by
>>>> SerialInstrument. Hence I type:
>>>>
>>>> class myClass(SerialInstrument)
>>>> self.clear(self)
>>>> def f1(self, str1, str2)
>>>> ...do something etc.
>>>>
>>>> I then get the message "self not know" from the statement
>>>> self.clear(self). I
>>>> have tried many other notations - none worked. What works is however
>>>> the
>>>> following code - specifying myClass without the self.clear(self) in it:
>>>>
>>>> x = myClass("argument")
>>>> x.clear()
>>>>
>>>> How can I integrate this call into the definition of myClass? Thanks
>>>> for advice.
>>>> Bob
>>>>
>>>>
>>>>
>>>
>>> By initialize, I'll assume you want this code to execute when your class
>>> is instantiated. The way to do that is with a method called __init__().
>>> Notice the double underscore at begin and end.
>>>
>>> class myClass(SerialInstrument):
>>> def __init__(self):
>>> self.val1 = 42
>>> self.val2 = 31
>>> #... also initialize the base class
>>> self.clear()
>>>
>>> def f1(self, str1, str2):
>>> ....
>>>
>>> You should also call the __init__() method of the base class. But I
>>> don't know whether you're using Python2 or Python3, so I won't write
>>> that call
>>>
>>> This is without knowing anything about your base class, so there may be
>>> many other adjustments to be made.
>>>
>>>
>>>
>>>
>> I defined (which should clear visa-buffers when instantiating the class):
>>
>> class myvisa(visa.SerialInstrument):
>> def __init__ (self):
>> self.clear()
>
> I still don't see the call to the superclass __init__(). Get that code
> from Bob Brusa's message.
>
>>
>> def io (self, printstr, askstr):
>> ...cut
>> when I run (python 2.7) a program using this class I get this:
>>
>> C:\Projekte\TDSsw\mypython>python chk_clear_1.py
>> Traceback (most recent call last):
>> File "chk_clear_1.py", line 8, in <module>
>> from myvisa import *
>> File "C:\Projekte\TDSsw\mypython\myvisa.py", line 15, in <module>
>> class myvisa(visa.SerialInstrument):
>> File "C:\Projekte\TDSsw\mypython\myvisa.py", line 121, in myvisa
>> visa.Instrument.clear()
>> TypeError: unbound method clear() must be called with Instrument
>> instance as first argument (got nothing instead)
>>
>> would it help to define instead:
>>
>> class myvisa(visa.SerialInstrument):
>> def __init__ (self):
>> x = SerialInstrument(self)
>> x.clear() #and then forget about this x?
>>
>> def io (self, printstr, askstr):
>> ...cut
>>
>
> Besides being indented wrong (did you even try it ?), that code doesn't
> begin to be what you want. You're calling clear on some other instance,
> then throwing that instance away, and not clearing the one you just
> created.
>
> But as I said before, you haven't said word-one about what the base
> class looks like, or how it's supposed to be used, nor when clear() is
> supposed to be called.
>
> You also aren't showing us the code which got the error, so I can't see
> how we could help. What does the code around line 115 look like? Is it
> part of the same class definition?
>
>
>
Dave,
to make it more clear, I attach a cut-down version of my program. It
includes comments to explain in more detail what my problem is.
Bob
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: inheritance and how to use it Bob Brusa <bob.brusa@gmail.com> - 2013-02-15 19:28 +0100
csiph-web