[Baypiggies] Trivial OOP pattern problem
dav9dg at gmail.com
Fri Jun 15 08:56:39 CEST 2012
The problem with this is if a method needs to return a value other than
self, you break it.
This is probably hacky, but you could accomplish what you want doing the
def wrapper(self, *args, **kwargs):
to_return = func(self, *args, **kwargs)
if to_return is None:
to_return = self
for method_name, method in inspect.getmembers(cairo.Context,
setattr(cls, method_name, return_self_decorator(method))
This way methods that return something other than None are not broken.
On Thu, Jun 14, 2012 at 11:28 PM, Brian Palmer <bpalmer at gmail.com> wrote:
> 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.
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Baypiggies