Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38932

Re: inheritance and how to use it

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'initialize': 0.05; 'class,': 0.07; 'advice.': 0.09; 'specifying': 0.09; 'underscore': 0.09; 'worked.': 0.09; 'def': 0.10; 'assume': 0.11; '11:59': 0.16; 'adjustments': 0.16; 'notations': 0.16; 'type:': 0.16; 'wrote:': 0.17; 'module': 0.19; 'made.': 0.22; 'class.': 0.23; 'statement': 0.23; 'downloaded': 0.24; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'integrate': 0.27; 'things,': 0.29; 'definition': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'code': 0.31; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'thanks': 0.34; 'offered': 0.35; "won't": 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'method': 0.36; 'anything': 0.36; "i'll": 0.36; 'should': 0.36; 'execute': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'notice': 0.39; 'build': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'received:74.208': 0.71; 'received:74.208.4.194': 0.84; 'bob': 0.91; 'net.': 0.91
Date Fri, 15 Feb 2013 12:11:01 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: inheritance and how to use it
References <511E6971.5040109@gmail.com>
In-Reply-To <511E6971.5040109@gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:aE2gLJFdaoPqsK/ib42U0DVdrHLqv19Bn8/SElPeKFo Wr5LLYAuf5h4ejO/BLakuHL2b0l5AF0oVpEeE6lR2733Sv350G sP5dr+mxod2EIvR0BEEMPbNcOgVkXtWVFlLNSODMK8SeAmbFT4 dthtV4cR5BWELlxzpRQfftgmPgtKEP4b9u6l1IAqZhlx9oXOGH pAytHeejYmezMZP5LzAAfmwczbfxAtcBHlrH70MM9xpesBWnhB vPSYStiLoPFAv3t0PEKaZELC5can+rusrEtWOv2IS1essU0/Ow W0DBv5SOabK7nLCQh+62aahkCTiJPsONIRTW6bTCdbjtm/pfQ= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1821.1360948284.2939.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360948284 news.xs4all.nl 6882 [2001:888:2000:d::a6]:60915
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38932

Show key headers only | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: inheritance and how to use it Dave Angel <davea@davea.name> - 2013-02-15 12:11 -0500

csiph-web