Indirect function member calls 
Author Message
 Indirect function member calls

Can anyone give me a better solution to this problem?  Given a class with
member functions A() and B(), I want to write a third function AorB() that
calls A() or B() in an extensible fashion.

Here's an example of what I have now:
    class Test:
       def A(Self):
          <do A's stuff>

       def B(Self):
          <do B's stuff>

       def AorB(Self, Method):
          eval( "Self.%s()" % (Method) )

This works, but I'd rather not pay the eval() overhead.  Is there a dictionary
with the class methods in it that I can reference by name:

       def AorB(Self, Method):
          Self.__method_dict__( Method )

And, before anyone beats me to it, I don't want to code up if-statements.
If I add new methods to be called, I don't want to have the change the
caller.

Thanks in advance,
John

--
+------------------+----------------------------------------------------------+
| _______          |                                                          |
| \____  |Advanced | John F. Croix         Advanced Architectural Development |
|  /|  | |Micro    | 5900 E Ben White                  Member Technical Staff |
| | |__| |Devices, | M/S 615 Austin, TX                         (512)602-1227 |

+------------------+----------------------------------------------------------+



Sat, 15 May 1999 03:00:00 GMT  
 Indirect function member calls

In article

   Can anyone give me a better solution to this problem?  Given a class with
   member functions A() and B(), I want to write a third function AorB() that
   calls A() or B() in an extensible fashion.

   Here's an example of what I have now:
       class Test:
          def A(Self):
             <do A's stuff>

          def B(Self):
             <do B's stuff>

          def AorB(Self, Method):
             eval( "Self.%s()" % (Method) )

   This works, but I'd rather not pay the eval() overhead.  Is there a dictionary
   with the class methods in it that I can reference by name:

          def AorB(Self, Method):
             Self.__method_dict__( Method )

   And, before anyone beats me to it, I don't want to code up if-statements.
   If I add new methods to be called, I don't want to have the change the
   caller.

Both:

          def AorB(Self, Method):
             Self.__dict__[Method]()

and

          def AorB(Self, Method):
             getattr(Self,Method)()

will work.  I prefer the latter.

--
Jim Fulton         Digital Creations



Sat, 15 May 1999 03:00:00 GMT  
 Indirect function member calls

Thanks for the tip.  The first method didn't work, but the second did (I'm
using 1.3, if that makes a difference).

Regards,
John
--
+------------------+----------------------------------------------------------+
| _______          |                                                          |
| \____  |Advanced | John F. Croix         Advanced Architectural Development |
|  /|  | |Micro    | 5900 E Ben White                  Member Technical Staff |
| | |__| |Devices, | M/S 615 Austin, TX                         (512)602-1227 |

+------------------+----------------------------------------------------------+



Sat, 15 May 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. indirect function calls and variable variables

2. indirect function calls and variable variables

3. Question regarding indirect function calls

4. Calling class member functions from C++

5. Calling C++ object member functions from ass ?K

6. Calling member functions?

7. VC++ calling fortran function and fortran function calling a c++ function

8. How to find out name of calling function from called function

9. Q: Interfacing C/ADA: Indirect Calls

10. Indirect calls using MASM 6.1 (how?)

11. indirect call with MASM?

12. Help - indirect call

 

 
Powered by phpBB® Forum Software