On Fri, Jul 15, 2011 at 5:41 AM, DR0ID <[email protected]> wrote:

> **
> On 15.07.2011 13:52, [email protected] wrote:
>
>   On Thu, 14 Jul 2011 13:57 -0700, "Michael Carius"
> <[email protected]> <[email protected]> wrote:
>
> # Python 2.x code
> class MySurface(object):
>     def __init__(self, obj):
>         if isinstance(obj, tuple):
>             self._surf = pygame.Surface(*tuple)
>         elif isinstance(obj, pygame.Surface):
>             self._surf = obj
>     def __getattr__(self, attr):
>         return getattr(self._surf, attr)
>
>
>  This looks the most promising, but I couldn't actually get it to work.
> What I want to do, is add extra attributes to a pygame.Surface object, such
> as pos and size, including _abs attributes that find the position relative
> to the screen. I'm using this in my GUI toolkit (which I talked about doing
> for GSoC earlier in the year). You can see the current surface.py module at
> http://bazaar.launchpad.net/~dreamsorcerer/simplegc/trunk/view/head:/sgc/surface.py
>
>  This works perfectly if I create a new surface by passing in a tuple, but
> I'm not sure how to get the same effect by passing in an existing
> pygame.Surface object.
>
>  Thanks,
>  Sam Bull
>
>
>
> Hi
>
> Peeking through your code I would suggest to use composition instead of
> inheritance. It is more flexible, but might look a bit more complex first.
> And personally I would think twice before overriding __getattr__.
>
> You know, in the newer pygame version there is a copy method ( see:
> http://www.pygame.org/docs/ref/surface.html#Surface.copy).
>
> If you still prefere to use inheritance and make an SurfaceObject, then
> maybe you could use the Surface.copy method and a metaclass to create a
> fancy SurfaceObject with the desired behavior.
>
>
> ~DR0ID
>

Looking at your code, it looks like you want a way to apply "filters" to the
public data stored in a surface. If that's the case, you could actually do
away with a custom class entirely, as Greg suggested, and define these
filters as a suite of helper functions: pos(surf), pos_abs(surf),
size(surf), etc. Each getter would be only one character longer than a
dot-access, and they could even have a value= keyword argument that would
turn them into setters. Most importantly, this solution is simple -- no
weird inheritance trees or __getattr__ redirections or constructor hacks or
metaclasses needed since it's still just a pygame surface.

-- 
Michael

Reply via email to