Python question: Calling a class method from within another class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michalml
    New Member
    • Apr 2007
    • 2

    #1

    Python question: Calling a class method from within another class

    I have the following simple structure:
    Code:
    class main_class(...):
    ...
    	def loadTests(...):
    
    	   string1 = "name_of_class"
    	   string2 = "function_in_class"
               string1.string2(p1,p2,...) #wish to do this but doesn't work..
    
    ...
    
    class name_of_class
    
    	def function_in_class(self, p1, p2 ,...)
    	....
    ...
    Is there any way that, from the class main_class in function loadTests , I can call the function in name_of_class
    ie. something like string1.string2 (p1, p2, ....) ? I've seen something similar done before, but can't quite
    figure it out. Thanks for your help!
    Last edited by bartonc; Apr 21 '07, 04:33 AM. Reason: added [code][/code] tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by michalml
    I have the following simple structure:
    Code:
    class main_class(...):
    ...
    	def loadTests(...):
    
    	   string1 = "name_of_class"
    	   string2 = "function_in_class"
               string1.string2(p1,p2,...) #wish to do this but doesn't work..
    
    ...
    
    class name_of_class
    
    	def function_in_class(self, p1, p2 ,...)
    	....
    ...
    Is there any way that, from the class main_class in function loadTests , I can call the function in name_of_class
    ie. something like string1.string2 (p1, p2, ....) ? I've seen something similar done before, but can't quite
    figure it out. Thanks for your help!
    Yep. Pass an instance of the working class to the overseer class (works with classes too, but I like to instanciate outside at pass it in):
    Code:
    class OverSeerClass:
        def __init__(self, workerClassInstance):
            self.worker = workerClassInstance
    
        def CallWorkerClassFunc(self):
            self.worker.DoWork()
    
    
    class WorkerClass:
        def DoWork(self):
            print "Working"
    
    
    w = WorkerClass()
    
    test = OverSeerClass(w)
    test.CallWorkerClassFunc()
    Please not the [CODE] tags instructions in the "REPLY GUIDELINES" on the right hand side of the page (while replying).

    Thanks for joining.

    Also be aware that I will rename this thread as soon as you reply. There are some helpful tips in the site's Posting Guidelines regarding this and other rules.

    Comment

    • michalml
      New Member
      • Apr 2007
      • 2

      #3
      Thanks for your very quick help! It FINALLY works ...

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by michalml
        Thanks for your very quick help! It FINALLY works ...
        You are welcome. That's what the Python Forum on TheScripts.com is here for.

        Welcome to it and keep posting,
        Barton

        Comment

        Working...