473,394 Members | 1,944 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,394 software developers and data experts.

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.__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.
Jul 7 '08 #1
6 6244
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.__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
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(OldStyle, 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.__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
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(OldStyle, 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
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...
3
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...
0
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
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...
13
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
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...
4
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;...
6
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
7
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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...

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.