473,789 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically Changing the Base Class

We have a situation where we want a Swig-generated Python class to
have a different base (not object). It doesn't appear that we can
coerce Swig into generating the class we want at present (but we are
still enquiring).

Is it possible to dynamically change the base class to something else?
Initial experiments appear to show it is not:
-------------------------------- snip --------------------------------
>>class Foo(object):
pass
>>class Foozle(object):
pass
>>Foozle.__base s__ = (Foo,)
Traceback (most recent call last):
File "<pyshell#6 >", line 1, in <module>
Foozle.__bases_ _ = (Foo,)
TypeError: __bases__ assignment: 'Foo' deallocator differs from
'object'
-------------------------------- snip --------------------------------

Is there a solution I am missing?

Thanks in advance.
Jul 7 '08 #1
6 6276
On Jul 7, 9:31*am, "Adam C." <adam...@gmail. comwrote:
We have a situation where we want a Swig-generated Python class to
have a different base (not object). It doesn't appear that we can
coerce Swig into generating the class we want at present (but we are
still enquiring).

Is it possible to dynamically change the base class to something else?
Initial experiments appear to show it is not:
-------------------------------- snip -------------------------------->>>class Foo(object):

* * * * pass
>class Foozle(object):

* * * * pass
>Foozle.__bases __ = (Foo,)

Traceback (most recent call last):
* File "<pyshell#6 >", line 1, in <module>
* * Foozle.__bases_ _ = (Foo,)
TypeError: __bases__ assignment: 'Foo' deallocator differs from
'object'
-------------------------------- snip --------------------------------

Is there a solution I am missing?

Thanks in advance.
Supposedly it should (usually) work, there's a 6 year old patch for
this (http://bugs.python.org/issue635933). Check if Swig can generate
old-style classes (i.e. not inheriting from object) since __bases__
assignment works for them.

HTH,
George
Jul 7 '08 #2
On Jul 7, 9:11*am, George Sakkis <george.sak...@ gmail.comwrote:
On Jul 7, 9:31*am, "Adam C." <adam...@gmail. comwrote:
We have a situation where we want a Swig-generated Python class to
have a different base (not object). It doesn't appear that we can
coerce Swig into generating the class we want at present (but we are
still enquiring).
Is it possible to dynamically change the base class to something else?
Initial experiments appear to show it is not:
-------------------------------- snip -------------------------------->>class Foo(object):
* * * * pass
>>class Foozle(object):
* * * * pass
>>Foozle.__base s__ = (Foo,)
Traceback (most recent call last):
* File "<pyshell#6 >", line 1, in <module>
* * Foozle.__bases_ _ = (Foo,)
TypeError: __bases__ assignment: 'Foo' deallocator differs from
'object'
-------------------------------- snip --------------------------------
Is there a solution I am missing?
Thanks in advance.

Supposedly it should (usually) work, there's a 6 year old patch for
this (http://bugs.python.org/issue635933). Check if Swig can generate
old-style classes (i.e. not inheriting from object) since __bases__
assignment works for them.

HTH,
George
Thanks. I think we would want new-style classes, and 6-year-old
patches strike me as maybe a little out of the desired path... so this
really just doesn't work in modern Python?
Jul 7 '08 #3
On Jul 7, 9:31 am, "Adam C." <adam...@gmail. comwrote:
We have a situation where we want a Swig-generated Python class to
have a different base (not object). It doesn't appear that we can
coerce Swig into generating the class we want at present (but we are
still enquiring).

Is it possible to dynamically change the base class to something else?
Initial experiments appear to show it is not:
-------------------------------- snip -------------------------------->>class Foo(object):

pass
>class Foozle(object):

pass
>Foozle.__bases __ = (Foo,)

Traceback (most recent call last):
File "<pyshell#6 >", line 1, in <module>
Foozle.__bases_ _ = (Foo,)
TypeError: __bases__ assignment: 'Foo' deallocator differs from
'object'
-------------------------------- snip --------------------------------

Is there a solution I am missing?

Thanks in advance.
You get the same effect as subclassing by using multiple inheritance.
Suppose your SWIG class called Foo to have a base class of Base. You
could instead call the SWIG class _Foo, and define Foo as a Python
class like this:

class Foo(_Foo,Base): pass

If you do that, the resulting class will behave exactly as if _Foo
were a subclass of base (apart from some introspection methods and
edge cases). You can see that this is so if you look at the MRO:

Foo, _Foo, Base, object

Which is the same MRO that would occur if _Foo derived from Base
directly.

The drawback (which is also a drawback to the hypothetical base
reassignment) is that Foo's __init__ method won't call Base's. The
only way you can make SWIG __init__ call it's superclass's __init__ is
apparently a patch.
Carl Banks
Jul 7 '08 #4
On Jul 7, 8:08*pm, "Adam C." <adam...@gmail. comwrote:
Thanks. I think we would want new-style classes, and 6-year-old
patches strike me as maybe a little out of the desired path... so this
really just doesn't work in modern Python?
Can you use (multiple) inheritance instead of changing the bases?
Alternatively, try using an old-style class, changing the bases
and subclassing it inheriting from object too:

class NewStyle(OldSty le, object):
pass

Jul 8 '08 #5
On Jul 7, 4:04*pm, Carl Banks <pavlovevide... @gmail.comwrote :
On Jul 7, 9:31 am, "Adam C." <adam...@gmail. comwrote:
We have a situation where we want a Swig-generated Python class to
have a different base (not object). It doesn't appear that we can
coerce Swig into generating the class we want at present (but we are
still enquiring).
Is it possible to dynamically change the base class to something else?
Initial experiments appear to show it is not:
-------------------------------- snip -------------------------------->>class Foo(object):
* * * * pass
>>class Foozle(object):
* * * * pass
>>Foozle.__base s__ = (Foo,)
Traceback (most recent call last):
* File "<pyshell#6 >", line 1, in <module>
* * Foozle.__bases_ _ = (Foo,)
TypeError: __bases__ assignment: 'Foo' deallocator differs from
'object'
-------------------------------- snip --------------------------------
Is there a solution I am missing?
Thanks in advance.

You get the same effect as subclassing by using multiple inheritance.
Suppose your SWIG class called Foo to have a base class of Base. *You
could instead call the SWIG class _Foo, and define Foo as a Python
class like this:

class Foo(_Foo,Base): pass

If you do that, the resulting class will behave exactly as if _Foo
were a subclass of base (apart from some introspection methods and
edge cases). *You can see that this is so if you look at the MRO:

Foo, _Foo, Base, object

Which is the same MRO that would occur if _Foo derived from Base
directly.

The drawback (which is also a drawback to the hypothetical base
reassignment) is that Foo's __init__ method won't call Base's. *The
only way you can make SWIG __init__ call it's superclass's __init__ is
apparently a patch.

Carl Banks
Thanks. Looks like I will just write a patch step and hook it into the
makefile.
Jul 8 '08 #6
On Jul 7, 10:44*pm, Michele Simionato <michele.simion ...@gmail.com>
wrote:
On Jul 7, 8:08*pm, "Adam C." <adam...@gmail. comwrote:
Thanks. I think we would want new-style classes, and 6-year-old
patches strike me as maybe a little out of the desired path... so this
really just doesn't work in modern Python?

Can you use (multiple) inheritance instead of changing the bases?
Alternatively, try using an old-style class, changing the bases
and subclassing it inheriting from object too:

class NewStyle(OldSty le, object):
* pass
Definitely don't want old-style classes. We could probably use
multiple inheritance, but it feels like a horrible hack to me; I'd
just go with duck typing and alternate implementations over that.
Jul 9 '08 #7

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

Similar topics

1
4346
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an inherited base member's access level in the derived class: its access level in the base class and the type of inheritance (public, protected, or private). After this determination is made, the following possibilities exist for manually changing the...
3
2138
by: Mikael Olofsson | last post by:
Hi! I've asked Google, but have not found any useful information there. Situation: I have a base class, say >>> class base(object): ImportantClassAttribute = None Now, I want to dynamically generate subclasses of base. That's not a
0
2119
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
1
1022
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a variable (targetId) in to the usercontrol (IntergySite.aspx) by calling its setter method. Currently, I am using if-then-else and hardcoded the User Control Object to do casting and call the setter method. Question: Is there any way I could load,...
13
2848
by: dragoncoder | last post by:
Consider the following code #include <iostream> class Base { public: virtual void say() { std::cout << "Base" << std::endl; } }; class Derived: public base {
4
1491
by: Andrew Backer | last post by:
Hello, I am having a problem creating a class dynamically. The class I have is a base class of another, and the parent class has the constructor (which takes one argument). The base class (Class1, below) does not have any constructors. I am using code like this to create it, and it's blowing up : Dim res As Object = Activator.CreateInstance( _ GetType( MyNameSpace.Class1 ), _
4
3304
by: Tugrul HELVACI | last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ?? I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String; fPersonSurName : String; fPersonAge : Integer; published property PersonName : String read fPersonName write fPersonName;
6
2559
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
7
6672
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: http://www.asp.net/learn/ajax-videos/video-156.aspx (3rd comment - Joe Stagner)
0
9666
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
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
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...
0
6769
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
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
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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.