Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38932 > unrolled thread
| Started by | Dave Angel <davea@davea.name> |
|---|---|
| First post | 2013-02-15 12:11 -0500 |
| Last post | 2013-02-15 12:11 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: inheritance and how to use it Dave Angel <davea@davea.name> - 2013-02-15 12:11 -0500
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-02-15 12:11 -0500 |
| Subject | Re: inheritance and how to use it |
| Message-ID | <mailman.1821.1360948284.2939.python-list@python.org> |
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.
--
DaveA
Back to top | Article view | comp.lang.python
csiph-web