[Baypiggies] Trivial OOP pattern problem

Isaac isaac at overttone.com
Fri Jun 15 08:17:33 CEST 2012


I'm not an expert Pythonista but my first pass would be something like the
following:

import cairo as C
# I don't know where surface is coming from
def make_my_shape_now(surface, color=(1.0, 1.0, 1.0), rect_x=0.0,
rect_y=0.0, rw=0.0, rh=0.0, fill=True):
    ctx = C.Context(surface)
    r, g, b = color
    ctx.set_source_rgb(r, g, b)
    ctx.rectangle(0.0, 0.0, rw, rh)
    ctx.fill()
    return ctx

my_shape = make_my_shape_now(some_surface, rw=float(rw), rh=float(rh))

my_other_shape = make_my_shape_now(some_surface, rw=float(rw), rh=float(rh),
 fill=False)

# etc.

If you would like to make a class, just make the above function a method on
a class that subclasses Context.

It sounds like you want a short cut to override the return value of most or
all of the Context methods that return None. I don't know of a way to do
that. The above is a short cut when you know which methods you want to use.

In addition to the above route, take a look at functools.partial

http://docs.python.org/library/functools.html#functools.partial

That might be what you want to use once you know the parameters that won't
change ( or change very often ).

I hope that helps!
Cheers,
Isaac

On Fri, Jun 15, 2012 at 1:52 AM, 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?
>
> --
> Ian Zimmerman
> gpg public key: 1024D/C6FF61AD
> fingerprint: 66DC D68F 5C1B 4D71 2EE5  BD03 8A00 786C C6FF 61AD
> http://www.gravatar.com/avatar/c66875cda51109f76c6312f4d4743d1e.png
> Rule 420: All persons more than eight miles high to leave the court.
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20120615/23a133ac/attachment.html>


More information about the Baypiggies mailing list