Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; '"""': 0.05; '(python': 0.05; 'initialize': 0.05; 'sys': 0.05; '(self,': 0.07; 'class,': 0.07; 'except:': 0.07; 'exit': 0.07; 'filename:fname piece:py': 0.07; 'received:192.168.178': 0.07; 'try:': 0.07; '(self):': 0.09; '__init__': 0.09; 'advice.': 0.09; 'called.': 0.09; 'cmd': 0.09; 'res': 0.09; 'specifying': 0.09; 'typeerror:': 0.09; 'underscore': 0.09; 'worked.': 0.09; 'def': 0.10; 'assume': 0.11; '11:59': 0.16; 'adjustments': 0.16; 'created.': 0.16; 'history:': 0.16; 'instead:': 0.16; 'notations': 0.16; 'program"': 0.16; 'type:': 0.16; 'unbound': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'instance,': 0.17; '>>>': 0.18; 'module': 0.19; 'define': 0.20; 'skip:" 30': 0.20; 'import': 0.21; 'supposed': 0.21; 'made.': 0.22; 'help.': 0.22; 'defined': 0.22; '15,': 0.23; 'class.': 0.23; 'statement': 0.23; 'this:': 0.23; "haven't": 0.23; 'downloaded': 0.24; 'tried': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; '(which': 0.26; 'skip:m 30': 0.26; '(most': 0.27; 'am,': 0.27; 'used,': 0.27; 'besides': 0.27; 'executing': 0.27; 'integrate': 0.27; "doesn't": 0.28; 'run': 0.28; '>>>>': 0.29; 'things,': 0.29; 'definition': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'attach': 0.30; 'code': 0.31; 'file': 0.32; 'could': 0.32; 'print': 0.32; 'message.': 0.33; 'comments': 0.33; "aren't": 0.33; 'traceback': 0.33; 'handle': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'version': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'wrong': 0.34; 'thanks': 0.34; 'clear': 0.35; 'offered': 0.35; 'pm,': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'next': 0.35; 'explain': 0.36; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'method': 0.36; 'anything': 0.36; "i'll": 0.36; 'should': 0.36; 'execute': 0.37; 'does': 0.37; 'being': 0.37; 'detail': 0.37; 'skip:v 20': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'notice': 0.39; 'build': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'help': 0.40; 'your': 0.60; 'first': 0.61; 'is.': 0.62; 'between': 0.63; 'more': 0.63; 'charset:windows-1252': 0.65; 'visa': 0.66; 'header:Reply- To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'reply- to:addr:gmail.com': 0.79; 'away,': 0.84; 'bob': 0.91; 'clearing': 0.91; 'net.': 0.91; 'instrument': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:reply-to:user-agent:mime-version:to :subject:references:in-reply-to:content-type; bh=bYT5hmKVAhZausqVr1RS/IQl0tkXbsGAgQubRwqEOaw=; b=mMNxmF0aqyGtzsx01XGDwpsx4+BUEeYBbapKyOq2SiUVtOlwMBktyaU3aH+agGDZmv 26O4eEq6CRe783pmwxOVQbFlS8Kxky4PhuS1qxzmBKm3iD6chOnGl2SpDeOioXjX85vR vdtX5wdPYPg1uQXuT92ncZJh1vN3ZOENahSytrIsNBC2KF7lfNe8yYw/CkC59F3hHe7y D5H3av3TaE/5Z1QEjqyO5S2kbI1WeCfen8upv6mSQoy3rcp4Xwossb4PAUwUue4kmZqX sga5Du7AQ9SimPWYocj6J1IFHUCbOHV4HbL2J2qVKDOQwsoUQqiFzMJ1BMGva+WXFs97 w3aw== X-Received: by 10.14.225.72 with SMTP id y48mr10827298eep.45.1360952930892; Fri, 15 Feb 2013 10:28:50 -0800 (PST) Date: Fri, 15 Feb 2013 19:28:50 +0100 From: Bob Brusa User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130107 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> <511E6C25.9020502@davea.name> <511E6F05.1090201@gmail.com> <511E7885.8060900@davea.name> In-Reply-To: <511E7885.8060900@davea.name> Content-Type: multipart/mixed; boundary="------------030309030803050102030103" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: bob.brusa@gmail.com List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 168 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360952932 news.xs4all.nl 6858 [2001:888:2000:d::a6]:53101 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38944 This is a multi-part message in MIME format. --------------030309030803050102030103 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 >> from myvisa import * >> File "C:\Projekte\TDSsw\mypython\myvisa.py", line 15, in >> 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 --------------030309030803050102030103 Content-Type: text/plain; charset=windows-1252; name="q1.py" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="q1.py" """/ edit history: 15-02-2013 Creation """ import sys import visa import time class myvisa(visa.SerialInstrument): """provide utilities to handle the instrument """ # def __init__ (): this doe not work # self.clear() def io (self, printstr, askstr): """combines a print and an ask-string into a single call """ res = 0 try: antw = self.ask(askstr) except: antw = "time-out" res = 1 print printstr + antw return res strich = " | "; pfeil = "->" print "start of program q1.py" iodev = "COM9" tds = myvisa(iodev, baud_rate = 57600) tds.clear() # this call I want to be called automatically when instantiating the class cmd = "*idn?"; tds.io(pfeil+cmd+strich,cmd) cmd = "status?"; tds.write(cmd) # exit with something in the visa-buffer and this will # lead to a de-chronization between write and reads when # executing a next program print "end of program" --------------030309030803050102030103--