473,854 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extending a class on runtime

Hi,

Let's say you have a bunch of instatiated objects of the same class on
your hands and you want to had some functionality to them.

I'm facing this situation while working with PyGTK and libglade to
create a GUI. Libglade creates a whole object tree representing the
GUI out of an XML file, and a bunch of GtkComboBox objects are
instantiated. I don't like the way GtkComboBox objects works, so I'd
like them to have some extra methods. Inheriting and extending
GtkComboBox is pointless because I'm not the one instantiating the
class. I only came up with three possibilities:

A) Adding my methods to the objects in a for-loop

B) Adding my methods to the GtkComboBox class (I tried this and it
seems to work)

C) Create a GtkComboBoxExte nded class inheriting from GtkComboBox
and change the instances' class in a for-loop.

I'm kind of inclined to C. B sounds dangerous and A is plain ugly.
I'm very new to this and I'm sure there is a well-established pythonic
way to solve this problem, so I'm appealing for your vast collective
wisdom to point me in the path of righteousness.

Thanks,
Rodrigo

Mar 27 '07 #1
1 1563
rd************@ gmail.com a écrit :
Hi,

Let's say you have a bunch of instatiated objects of the same class on
your hands and you want to had some functionality to them.
Then I'd just do it.
I'm facing this situation while working with PyGTK and libglade to
create a GUI. Libglade creates a whole object tree representing the
GUI out of an XML file, and a bunch of GtkComboBox objects are
instantiated. I don't like the way GtkComboBox objects works, so I'd
like them to have some extra methods. Inheriting and extending
GtkComboBox is pointless because I'm not the one instantiating the
class. I only came up with three possibilities:

A) Adding my methods to the objects in a for-loop

B) Adding my methods to the GtkComboBox class (I tried this and it
seems to work)
I don't have much experience with PyGTK, but unless GtkComboBox is a
very special class object, it should JustWork(tm).
C) Create a GtkComboBoxExte nded class inheriting from GtkComboBox
and change the instances' class in a for-loop.
That's another possible solution, but it requires more work.
I'm kind of inclined to C. B sounds dangerous
Why ? Eventually confusing if not well documented, but mostly harmless
IMHO. Python is dynamic by nature, and there's no reason to not take
advantage of this fact.
and A is plain ugly.
It's also the more complicated and less efficient solution IMHO (unless
you want different implementations of the same method on a per-instance
basis...).
I'm very new to this and I'm sure there is a well-established pythonic
way to solve this problem, so I'm appealing for your vast collective
wisdom to point me in the path of righteousness.
As far as I'm concerned, and if it's just a matter of adding a couple of
methods, I'd go for B, which is the SimplestThingTo Do(tm).

Now if it's a big complicated project with a rather unstable team, C is
perhaps a bit more explicit since it makes obvious that this not a
pristine GtkComboBox no more. But even then, it's always possible to
monkeypatch a class in a more or less documented way (explict mentions
of the patch in added/replaced/extended methods and in __repr__()).

My 2 cents...
Mar 27 '07 #2

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

Similar topics

4
3435
by: Matt | last post by:
Hi, I've been thinking about how to do this, but can't think of a solution. I have a class that is derived from System.Web.UI.WebControls.DataGrid which works a treat, but I'd like to extend the DataGridColumnCollection class (property of DataGrid) just a fraction by allowing a string indexer. i.e. rather than using myDataGrid.Columns.Visible = false;
8
2348
by: Martin Horn | last post by:
Hi, I am trying to add extra functionality to the standard RichText control, but I've fallen at the first hurdle,can someone take a look at the following code and tell me why it fails to return true if the current selection is bold. Thanks, Martin Horn.
3
2307
by: James | last post by:
Hi, I've been trying to figure this out for a while and can't find the answer. For simplicity I will use a basic example rather than my real classes. I have one base class, Item and various extended classes which inherit from Item, ExtendedItem1, ExtendedItem2, etc. As expected, Item defines properties and methods common to all items. The ExtendedItem classes define additional properties and methods specific to different types of items.
5
3837
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where confusion veered it's ugly head :( Learning Python explains on page 324: Class Interface Techniques (21.3.3 in the ebook) the following is an extender method. ''' #################################### '''
3
4790
by: Barry | last post by:
A beginners question to someone that isn't a beginner ;) I've forgotton a lot. The code below isn't what I want to do but it demonstrates succinctly my problem. I want to drop a "MyTextBox" control on a form at design time, set the "DummyProperty" property to be "test value1", and I want to see it showing at design and runtime "test value1". It shows a blank. If I do this with the "Text" property it works but not with my "DummyProperty"...
13
2128
by: interec | last post by:
I have some code in Java that I need to translate into C++. My Java code defines a class hierarchy as follows: // interface IA public interface IA { public abstract void doA(); } // interface IB
7
2249
by: ptdorf | last post by:
Hi everybody, one of the most powerful things about ruby is its ability to add funcionality to a class (or an instance of a class) at runtime. Can anyone please tell me if this can be done with php? thanks a million
0
766
by: J. Moreno | last post by:
Hello, I'm trying to add an extension to Object using VB. It shows up in the intellisense for other kinds of reference objects, but not if the variable is simply declared as Object. It works without problems when I use C#, but not from within VB...any ideas as to what is going on? Imports System.Runtime.CompilerServices
3
1308
by: J. Moreno | last post by:
Since nobody seems to know the answer in the vb group, guess I'll ask here... Does anyone know why I would be able to create an extension method for Object in C# but not in VB? I understand why using ByRef for the first parameter would work in VB but not C#, but this I just don't get at all... J. Moreno <planb@newsreaders.comwrote:
0
9899
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10672
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9509
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7909
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5936
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.