471,325 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Redirecting class method calls to owned objects

bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. #-----------------------------------------------------------------------------
  2. # Name:        ObjectRedirect.py
  3. # Purpose:     Demonstrate class methode redirection with new-style classes
  4. #
  5. # Author:      Barton
  6. #
  7. # Created:     2007/09/20
  8. # RCS-ID:      $Id: ObjectRedirect.py $
  9. # Copyright:   (c) 2007
  10. # Licence:     free, free, free
  11. #-----------------------------------------------------------------------------
  12.  
  13.  
  14. """In GUI programming, it is almost always the case that a simple widget needs
  15.    a parent window to be displayed in. No problem... Create the Frame() with a
  16.    sizer (usually) and add (say) a multi-line text widget. And there you have your
  17.    text display. Now, in the main application, create an instance of your new Frame
  18.    and call myFrame.Show() to get it on the screen. But, what about writing text to
  19.    that widget that is owned by the Frame??? You could:
  20.    self.myFrame.textObj.WriteText("the text to write")  # but that many dots can be worrisome
  21.    or
  22.    textObj = self.myFrame.GetTextObj()     # but there's that extra line
  23.    textObj.WriteText("the text to write")  # and you need to define GetTextObj()
  24.  
  25.    I've been toying with a trick that seems to work quite well on wxPython widgets that all
  26.    decend from a python object (new-style class objects). In the following snippet, imagine that these
  27.    objects are all several layers deep in the object hierarchy and decent from the same roots"""
  28.  
  29.  
  30. class SimulatedTextWidget(object):
  31.     """bla-bla_bla"""
  32.     def __init__(self, parent, *args, **kwargs):
  33.         self.parent = parent
  34.  
  35.     def WriteText(self, text):
  36.         print text
  37.  
  38.  
  39. class SimulatedFrame(object):
  40.     """bla-bla_bla"""
  41.     def __init__(self, parent, *args, **kwargs):
  42.         self.parent = parent
  43.         self.simTextObj = SimulatedTextWidget(self)
  44.  
  45.     def __getattribute__(self, name):
  46.         """Redirect method calls: Try this object first. If it's not a method/attribute
  47.            of this object, try the redirected object."""
  48.         try:
  49.             return object.__getattribute__(self, name)
  50.         except AttributeError:
  51.             return object.__getattribute__(self.simTextObj, name)
  52.  
  53.  
  54.  
  55. if __name__ == "__main__":
  56.     frame = SimulatedFrame(None)
  57.     frame.WriteText("Hello world!")
Sep 21 '07 #1
0 960

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Joseph Turian | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.