[Baypiggies] Trivial OOP pattern problem

Brian Palmer bpalmer at gmail.com
Fri Jun 15 08:28:36 CEST 2012

On Thu, Jun 14, 2012 at 10:52 PM, Ian Zimmerman <itz at buug.org> wrote:

> Isaac> Hi, Why do you need each of the methods to return the instance?
> Isaac> Can you just wrap the method calls in a function and return the
> Isaac> instance?
> I am not married to the idea of a wrapper class, but I don't understand
> what you propose instead.  What would the example code look like under
> your scheme?

Your initial solution seems like it'd work:
class Wrap(object):
  def __init__(self, ctx):
    self.ctx = ctx

  def __getattr__(self, name):
    def myf(*args, **kwargs):
      getattr(self.ctx, name)(*args, **kwargs)
      return self
    return myf

There are benefits to being less dynamic, but this might be good enough.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20120614/86dd7e09/attachment-0001.html>

More information about the Baypiggies mailing list