473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to change the superclass of an instance?

Hello,

I want to do the following. I have a function called display.set_mode()
which returns an instance of Surface. I want to add some bound
functions to this object therefore I thought to do something like:
class screen(Surface):

.... def __new__(cls):
.... a = display.set_mode()
.... a.__class__ = screen
.... return a
.... def new_func(self):
.... pass

My idea was that my new created object has an attribute resolution
order of instance -> screen -> Surface and therefore I can use all
Surface methods on the instance and the new_func(). But I got the error
message: "TypeError: __class__ assignment: only for heap types"

I also tried to add a bound function (to the returned Surface of
display.set_mode()) with setattr and the appropriate arguments but I
got the error message: pygame.Surface object has no attribute 'update'
so I think Surface is somehow implemented immutable by intercepting
attribute access via __getattribute__

My question, how can I realize that I described above, because my
attempt seems to be wrong.

An other idea I had would be wrapping a class around the Surface
returned by display.set_mode() and then intercepting attribute
references with __getattr__ on that class and if not found in class or
instance, delegate them to the Surface. I think this way it would look
liked the wrapping class is a subclass of Surface. But Surface don't
have a __dict__ and I don't know an other way to call a function by his
name.

Bye
Alex

Jul 18 '05 #1
1 2330
Alexander Stante <al*********@yahoo.de> wrote:
...
An other idea I had would be wrapping a class around the Surface
returned by display.set_mode() and then intercepting attribute
references with __getattr__ on that class and if not found in class or
Best (but, only attributes not found otherwise go to __getattr__, so, no
need for you to duplicate such checks).
instance, delegate them to the Surface. I think this way it would look
liked the wrapping class is a subclass of Surface. But Surface don't
have a __dict__ and I don't know an other way to call a function by his
name.


class generic_wrapper_base(object):
def __init__(self, wrappee): self.__dict__['_w'] = wrappee
def __getattr__(self, n): return getattr(self._w, n)
def __setattr__(self, n, v): setattr(self._w, n, v)
def __delattr__(self, n): delattr(self._w, n)

No need for the wrappee to have a __dict__: getattr, setattr and delattr
are the built-in functions you need. Happy wrapping!-)
Alex
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jim Newton | last post by:
hi all, does anyone know what print does if there is no __str__ method? i'm trying ot override the __repr__. If anyone can give me some advice it would be great to have. I have defined a...
2
by: Reid Priedhorsky | last post by:
Dear group, I'd have a class defined in one module, which descends from another class defined in a different module. I'd like the superclass to be able to access objects defined in the first...
2
by: rh0dium | last post by:
Hi all, Still a newbie but making some headway. So I have a file structure like this.. top/ --modules/ ----metrics.py --metrix/ ----uptime.py
2
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass'...
3
by: tac-tics | last post by:
I have a program which has a GUI front-end which that runs a separate thread to handle all the important stuff. However, if there is a problem with the important stuff, I want the GUI to raise a...
1
by: shivapadma | last post by:
1..In java we can refer superclass constructor by super()keyword,but Where as in c++ how can we refer to that???. In java the following code is used for referring superclass...
2
by: pvl_google | last post by:
Hi, I'm trying to extend an STL class with additional iterator functionality. In the simplified example below I added an extra iterator class with a dereferencing operator. This operator...
6
by: bill226 | last post by:
I have to get the subclass to use the superclass constructor, heres my code: public class Student { // instance variables private String name; private int pin; private...
2
by: Mark Brading | last post by:
Hi all, I am trying to declare a Map object with an abstract superclass (TableFields_v2) object as follows:- private Map<Integer, TableFields_v2> elementList; When I come to create the object...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.