cjbust
cjbust

Reputation: 123

Class definition and methods

I am attaching a listing of some code for a couple of class definition. My objective was to create a class with an event handler that could repeat a task when a Timer elapsed. In the TimerClass, I defined a method on_timed_event inside the class __init__. I instatiate a TimerTest class in main function a = TimerTest() and I discovered that this also executes the event handler.

Question 1. Is this a way to automatially execute functions when an object instance is created?

Question 2. I would like to pass ClassA.method1 to ClassB. Is the way I did it below in main () passing sm.add() Delay class correct? Is it possible to create and instance of Delay class and pass it the equivalent of class instance SimpleMath(X,Y) and add() method on the fly on one line? Lambda function?

import System  
from System.Timers import (Timer, ElapsedEventArgs)

class TimerTest(object):

    def __init__ (self):
        self.timer = Timer()
        self.timer.Interval= 1000
        self.timer.Enabled = True


        def on_timed_event (source, event):
            print 'event from TimerTest class'
            print "The Elapsed event was raised at " , event.SignalTime
            print '------------'

        self.timer.Elapsed += on_timed_event    


class Delay(object):

    def __init__(self,class_method):
       self.delay = Timer()
       self.delay.Interval= 2000
       self.method= class_method

    def on_timed_delay_event (self,sender, event):
       print 'from Delay Class event handler'
       print "event has elapsed event was raised at " , event.SignalTime
       print 'addition results',self.method
       print '------------'

    def start(self):
       print 'Delay timer Start'
       self.delay.Elapsed += self.on_timed_delay_event
       self.delay.Enabled= True


def stop(self):
self.delay.Enabled= False
self.delay.Elapsed -= self.on_timed_delay_event

class SimpleMath(object):

    def __init__(self,a,b):
        self.a =a
        self.b =b

    def add (self):
        return self.a + self.b

def main():

   a = TimerTest()
   sm= SimpleMath(10,12)
   print sm.add()

  t= Delay(sm.add())
  t.start()

if __name__ == '__main__':
   main()

I am adding more details to my question. Class Task and Request are imported into the module that contains the MainForm class. Inside the MainForm I create an instance of Request class which gets passed to Tab class which uses it and passes it to Task class which is supposed to perform a task when an event is triggered. In the condensed listing below, self.myRequest.command1 is only being execute once. I added a print after self.params is assigned inside execute and it prints None. Is there a particular way I should be passing self.myRequest.command1(arg1, arg2, arg3)?

class Task(object):
...
    def execute(self, param)
        self.params = param                             #method to be executed periodically
        print 'display self.params', self.params        #debugging message

class Request (object):
...
    def command1(self, arg1, arg2, arg3)

class Tab (object):
    __init__(self,tabControl, myRequest):
        self.myRequest= myRequest
        self.myTask = Task()
...
    def send_task(self):

        self.myTask.execute(self.myRequest.command1(arg1, arg2, arg3)) 

class MainForm(Form):
    __init__(self):
    ...
    self.myRequest= Request()
    self.tab=Tab(self.tabControl, self.myRequest)
    ...

Upvotes: 0

Views: 140

Answers (1)

prgao
prgao

Reputation: 1787

1.

class A:
  def some_func():
    pass

  def __init__(self):
    self.init = 1

    self.some_func() # just call member function

2.No, you don't need () to pass functions in as arguments class A: def some_func(): pass

def func_of_func(func):
  return fund()

def main():
  a = A()
  func_of_func(a.some_func) # no parentheses after some_func

Upvotes: 1

Related Questions