[cairo] pycairo subclass

Steven Chaplin stevech1097 at yahoo.com.au
Wed Dec 12 02:31:40 PST 2007


On Mon, 2007-12-10 at 12:36 -0800, cairo-request at cairographics.org
wrote:
> 
> Hi,
> 
> OK .. I understand that it's not a good idea to subclass cairo
> surfaces, so it's forbidden.
> But I found, one can easily overshoot this ban like that:
> 
> # ---------------- example begin ----------------
> import cairo
> 
> 
> class derived_context(object):
>         def __init__(self,w,h,fn):
>                 self.s=cairo.PDFSurface(fn, w, h)
>                 self.c=cairo.Context(self.s)
>         
>         # ----  inherited methods ----- 
>         def move_to(self,x,y):
>                 self.c.move_to(x,y)
>                 
>         def line_to(self,x,y):
>                 self.c.line_to(x,y)
> 
>         def rel_line_to(self,x,y):
>                 self.c.rel_line_to(x,y)
>         
>         def close_path(self):
>                 self.c.close_path()
> 
>         def stroke(self):
>                 self.c.stroke()
>                 
>         # ----  new methods -----
>         def triangle(self):
>                 self.rel_line_to(10,0)
>                 self.rel_line_to(-5,7)
>                 self.close_path()
> 
> if __name__== "__main__":
>         d=derived_context(100,100,'pokus.pdf')
>         d.move_to(50,50)
>         d.triangle()
>         d.stroke()
> 
> # ---------------- example end ----------------

This demonstrates one class X 'has-a' class Y, rather class X 'is-a'
class Y (subclass). Its an alternative method to subclassing, and is
useful when subclassing is not allowed, or when the new class is not
really a subclass.
 I think, in general, that since Python has nice support for classes,
there can be a tendency to want to subclass everything, even when
subclassing is not necessary.
 For cairo.Context is does seem reasonable to want to create a
subclass to add new drawing methods. So future versions of pycairo
will allow subclassing of cairo.Context.

> Does this approach have the problem with memory leaks too?
> I don't think so, but I'm new in python and I don't know much about
> wraping C library and realted problems.

I don't think there should be any memory leaks. I'd be interested
to see any examples which demonstrate pycairo memory leaks.

> It works ... but it's ugly. If this is the only way, it should be
> written(highlighted) on web page and documentation. The information
> about forbidden subclassing is inconceivably hidden.

Cairo, the C library, is not an object oriented library, so a Python
binding can never be a truly object oriented interface to cairo.
One way to write the Python bindings for cairo would be as a single
long list of module functions - this would be the most accurate
representation of the underlying C library. Pycairo (and most other
cairo language bindings?) instead chose to implement the bindings using
Context, Surface, Pattern, etc classes.
An advantage is that the classes organise cairo into groups of similar
functions.
A disadvantage is that creates an illusion that cairo is object
oriented library, and people are then tempted to create subclasses to
override cairo methods. When in fact there are no methods to
override, just cairo functions which can't be overridden.

I'll start a "FAQ" file, and add this to it.

Regards,
Steve
      



More information about the cairo mailing list