[cairo] I don't see the pattern

Kalle Vahlman kalle.vahlman at gmail.com
Thu Dec 6 23:41:12 PST 2007


2007/12/6, Carl Worth <cworth at cworth.org>:
> I know I've got that "cairo_create(ctx.cached_surface)" line
> wrong. With the python binding there's probably some ugly syntax like
> "cairo.CairoContext.new(ctx.cached_surface)" or something, but I can
> never remember what that looks like. In C the function is
> "cairo_create" which is the same thing that the window's
> "cairo_create" method is modeled after.

Now now, it's not _that_ ugly. 'cairo.Context(surface)' or even
'Context(surface)' if you import it to the namespace.

You could actually even fake the C syntax with

  from cairo import Context as cairo_create

but that might look funny if you have any static methods in the class
(I don't think there is though).

> Similarly, within create_similar you'll need a content value. In C,
> this is a simple enum like CAIRO_CONTENT_COLOR_ALPHA or
> CAIRO_CONTENT_COLOR. But again, in python, you have to have some
> mixed-syntax thing for the namespace like cairo.CONTENT_COLOR_ALPHA or
> whatever.

This I agree with, those are pretty yucky. The only solution here that
I can think of is to import all cairo stuff to the local namespace
with

  from cairo import *

which allows for

  surface = othersurface.create_similar(CONTENT_ALPHA, 100, 100)

but at the same time makes you worry about namespace pollution (which
can be very confusing since Python is not strongly typed).

-- 
Kalle Vahlman, zuh at iki.fi
Powered by http://movial.fi
Interesting stuff at http://syslog.movial.fi


More information about the cairo mailing list