Re: inheritance and how to use it
Am 15.02.2013 19:06, schrieb Dave Angel: 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 instantiatedwhich 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(). Hi Dave and Thomas, yep - now its working: See also attachment q4.py C:\Projekte\TDSsw\mypython>python q4.py start of program q4.py ->*idn? | "SPEAG","TDS","RUID:00.00-00.00 FW:2. SID:00.00-00.00 FW:01.00.00" end of program Thanks for your kind help - bob """/ edit history: 15-02-2013 Creation """ import sys import visa import time class myvisa(visa.SerialInstrument): """provide utilities to handle the instrument """ def __init__(self, addr): # accept all parameters super(myvisa, self).__init__(addr, baud_rate = 57600) self.clear() # Thomas does not think that self is to be given twice # he is right! 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 q4.py" iodev = "COM7" tds = myvisa(iodev) 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" -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance and how to use it
On 02/15/2013 01:28 PM, Bob Brusa wrote: 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 Good job cutting down the sample. Naturally, it'd have been smart to tell us you don't get the same traceback this way. But it looks like the first problem is in that missing __init__() method. Instead of saying "it doe not work" it'd be good to examine just what error traceback you got when you did have it there. I suspect it's because you're passing parameters in when you invoke it (iodev, baud_rate = 57600) but you don't have any arguments. You don't even have the self argument. You are also missing the super call, as outlined earlier in this thread. So the base class won't being properly initialized once you fix the first problem. Reread Thomas Rachel's message, and if you still can't make it work, tell us what errors you still get, or what you don't understand. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance and how to use it
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 """/ 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" -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance and how to use it
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 instantiatedwhich 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance and how to use it
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? -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance and how to use it
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 instantiatedwhich 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance and how to use it
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() 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance and how to use it
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance and how to use it
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 -- http://mail.python.org/mailman/listinfo/python-list
inheritance and how to use it
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 -- http://mail.python.org/mailman/listinfo/python-list