Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > inheritance and how to use it

Reply
Thread Tools

inheritance and how to use it

 
 
Bob Brusa
Guest
Posts: n/a
 
      02-15-2013
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
 
Reply With Quote
 
 
 
 
Thomas Rachel
Guest
Posts: n/a
 
      02-15-2013
Am 15.02.2013 17:59 schrieb Bob Brusa:
> 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).


Which is absolutely correct. Besides, I would have expected some syntax
errors.

You try to execute the clear() method during the definition of the
class, not during the instantiation.

Instantiation happens in the __init__() method.

You'll have to do it like this:

class myClass(SerialInstrument):
def __init__(self, *a, **k): # accept all parameters
super(myClass, self).__init__(*a, **k)
self.clear() # I don't think that self is to be given twice here...
def f1(self, str1, str2):
pass

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()


Here the clear() is called on the object which has been created, so
after calling the __init__() above (which is, roughly, equivalent to
calling it at the bottom of __init__()).


Thomas
 
Reply With Quote
 
 
 
 
Bob Brusa
Guest
Posts: n/a
 
      02-15-2013
Am 15.02.2013 18:06, schrieb Thomas Rachel:
> Am 15.02.2013 17:59 schrieb Bob Brusa:
>> 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).

>
> Which is absolutely correct. Besides, I would have expected some syntax
> errors.
>
> You try to execute the clear() method during the definition of the
> class, not during the instantiation.
>
> Instantiation happens in the __init__() method.
>
> You'll have to do it like this:
>
> class myClass(SerialInstrument):
> def __init__(self, *a, **k): # accept all parameters
> super(myClass, self).__init__(*a, **k)
> self.clear() # I don't think that self is to be given twice
> here...
> def f1(self, str1, str2):
> pass
>
> 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()

>
> Here the clear() is called on the object which has been created, so
> after calling the __init__() above (which is, roughly, equivalent to
> calling it at the bottom of __init__()).
>
>
> Thomas


Thomas,
This does not work either. The error comes while python analyses the
code - even prior to executing my program.... But what I want to achieve
is that this clear() is executed when the class is instantiated....which
I do with the code

x = myClass("COM7")

Of course, when scanning the class definition, the argument "COM7" is
not yet known.
Thanks for further help. Bob

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      02-15-2013
On 02/15/2013 12:50 PM, Bob Brusa wrote:
> Am 15.02.2013 18:06, schrieb Thomas Rachel:
>> Am 15.02.2013 17:59 schrieb Bob Brusa:
>>> 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).

>>
>> Which is absolutely correct. Besides, I would have expected some syntax
>> errors.
>>
>> You try to execute the clear() method during the definition of the
>> class, not during the instantiation.
>>
>> Instantiation happens in the __init__() method.
>>
>> You'll have to do it like this:
>>
>> class myClass(SerialInstrument):
>> def __init__(self, *a, **k): # accept all parameters
>> super(myClass, self).__init__(*a, **k)
>> self.clear() # I don't think that self is to be given twice
>> here...
>> def f1(self, str1, str2):
>> pass
>>
>> 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()

>>
>> Here the clear() is called on the object which has been created, so
>> after calling the __init__() above (which is, roughly, equivalent to
>> calling it at the bottom of __init__()).
>>
>>
>> Thomas

>
> Thomas,
> This does not work either. The error comes while python analyses the
> code - even prior to executing my program.... But what I want to achieve
> is that this clear() is executed when the class is instantiated....which
> I do with the code
>
> x = myClass("COM7")
>
> Of course, when scanning the class definition, the argument "COM7" is
> not yet known.
> Thanks for further help. Bob
>


Your error is on line 115, so what does it look like, and its context?
I expect you're never getting to the line x = myClass().

--
DaveA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
Builtn super() function. How to use it with multiple inheritance? Andwhy should I use it at all? Lacrima Python 43 08-02-2010 09:28 PM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57