473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to set a class inheritance at instance creation?

Hi I wonder if you can set what subclass a class should
have at instance creation.

The problem is that I have something like:

class CoreLang():
def AssignVar(self, var, value):
pass

class Lang1(CoreLang) :
def AssignVar(self, var, value):
return var, "=", value

class Lang2(CoreLang) :
def AssignVar(self, var, value):
return var, "<=", value

class WriteStruct():
def Generate(self, vars):
for var in vars:
print self.AssignVar( )

The problem is that I want WriteStruct to sometimes be a subclass of
Lang1 and sometimes
of Lang2.
In the above example I could but the Generate Method in CoreLang. But
in my real
example I also want to able to subclass WriteStruct to be able to easy
customize WriteStruct.
Which I wouldnt be able to do if it was a method in CoreLang.

So in code I would like to write something like:

WriteStruct(Lan g1).Generate(va rs)

Even better would be that if I in the Lang1 class could
just do WriteStruct().G enerate(vars) and Lang1 class would
magically make WriteStruct a subclass of itself.
Cheers,

/T

May 29 '07 #1
9 1213
On May 29, 8:52 pm, glomde <tbr...@yahoo.c omwrote:
Hi I wonder if you can set what subclass a class should
have at instance creation.

The problem is that I have something like:

class CoreLang():
def AssignVar(self, var, value):
pass

class Lang1(CoreLang) :
def AssignVar(self, var, value):
return var, "=", value

class Lang2(CoreLang) :
def AssignVar(self, var, value):
return var, "<=", value

class WriteStruct():
def Generate(self, vars):
for var in vars:
print self.AssignVar( )

The problem is that I want WriteStruct to sometimes be a subclass of
Lang1 and sometimes
of Lang2.
In the above example I could but the Generate Method in CoreLang. But
in my real
example I also want to able to subclass WriteStruct to be able to easy
customize WriteStruct.
Which I wouldnt be able to do if it was a method in CoreLang.

So in code I would like to write something like:

WriteStruct(Lan g1).Generate(va rs)
class WriteStruct:
def __init__(self, SubClass):
self._sub = SubClass()
def Generate(self, vars):
for var in vars:
print self._sub.Assig nVar()

This does what you want but isn't inheritance.
Even better would be that if I in the Lang1 class could
just do WriteStruct().G enerate(vars) and Lang1 class would
magically make WriteStruct a subclass of itself.
I don't think I understood what you want here.

Ram

May 29 '07 #2
Why not just have Lang1 and Lang2 inherit from WriteStruct as well?

On May 29, 8:52 am, glomde <tbr...@yahoo.c omwrote:
Hi I wonder if you can set what subclass a class should
have at instance creation.

The problem is that I have something like:

class CoreLang():
def AssignVar(self, var, value):
pass

class Lang1(CoreLang) :
def AssignVar(self, var, value):
return var, "=", value

class Lang2(CoreLang) :
def AssignVar(self, var, value):
return var, "<=", value

class WriteStruct():
def Generate(self, vars):
for var in vars:
print self.AssignVar( )

The problem is that I want WriteStruct to sometimes be a subclass of
Lang1 and sometimes
of Lang2.
In the above example I could but the Generate Method in CoreLang. But
in my real
example I also want to able to subclass WriteStruct to be able to easy
customize WriteStruct.
Which I wouldnt be able to do if it was a method in CoreLang.

So in code I would like to write something like:

WriteStruct(Lan g1).Generate(va rs)

Even better would be that if I in the Lang1 class could
just do WriteStruct().G enerate(vars) and Lang1 class would
magically make WriteStruct a subclass of itself.

Cheers,

/T

May 29 '07 #3
On 29 Maj, 19:27, "shand...@gmail .com" <shand...@gmail .comwrote:
Why not just have Lang1 and Lang2 inherit from WriteStruct as well?
This wont work I think since if add antoher Class:

class WriteStruct():

def func1(self);
print "Hello2"

def Generate(self):
self.func1()

class WriteStruct2(Wr iteStruct):

def func1(self);
print "Hello"

def Generate(self):
self.func1()

Den if Lang1, inherit both WriteStruct and WriteStruct2 I will
get name clashes.

In my real code I have very big Generate Method in WriteStruct that
calls submethods.
and thes submethods should be overriden by the subclasses. So I cant
change the name
on the submethods.
>
On May 29, 8:52 am, glomde <tbr...@yahoo.c omwrote:
Hi I wonder if you can set what subclass a class should
have at instance creation.
The problem is that I have something like:
class CoreLang():
def AssignVar(self, var, value):
pass
class Lang1(CoreLang) :
def AssignVar(self, var, value):
return var, "=", value
class Lang2(CoreLang) :
def AssignVar(self, var, value):
return var, "<=", value
class WriteStruct():
def Generate(self, vars):
for var in vars:
print self.AssignVar( )
The problem is that I want WriteStruct to sometimes be a subclass of
Lang1 and sometimes
of Lang2.
In the above example I could but the Generate Method in CoreLang. But
in my real
example I also want to able to subclass WriteStruct to be able to easy
customize WriteStruct.
Which I wouldnt be able to do if it was a method in CoreLang.
So in code I would like to write something like:
WriteStruct(Lan g1).Generate(va rs)
Even better would be that if I in the Lang1 class could
just do WriteStruct().G enerate(vars) and Lang1 class would
magically make WriteStruct a subclass of itself.
Cheers,
/T

May 29 '07 #4
On 29 Maj, 19:20, Ramashish Baranwal <ramashish.li.. .@gmail.com>
wrote:
On May 29, 8:52 pm, glomde <tbr...@yahoo.c omwrote:
Hi I wonder if you can set what subclass a class should
have at instance creation.
The problem is that I have something like:
class CoreLang():
def AssignVar(self, var, value):
pass
class Lang1(CoreLang) :
def AssignVar(self, var, value):
return var, "=", value
class Lang2(CoreLang) :
def AssignVar(self, var, value):
return var, "<=", value
class WriteStruct():
def Generate(self, vars):
for var in vars:
print self.AssignVar( )
The problem is that I want WriteStruct to sometimes be a subclass of
Lang1 and sometimes
of Lang2.
In the above example I could but the Generate Method in CoreLang. But
in my real
example I also want to able to subclass WriteStruct to be able to easy
customize WriteStruct.
Which I wouldnt be able to do if it was a method in CoreLang.
So in code I would like to write something like:
WriteStruct(Lan g1).Generate(va rs)

class WriteStruct:
def __init__(self, SubClass):
self._sub = SubClass()
def Generate(self, vars):
for var in vars:
print self._sub.Assig nVar()

This does what you want but isn't inheritance.
This would work I think. Thanks.
>
Even better would be that if I in the Lang1 class could
just do WriteStruct().G enerate(vars) and Lang1 class would
magically make WriteStruct a subclass of itself.

I don't think I understood what you want here.
I just dont want to pass the class in the instancecreatio n.
Somhehow in my:

class Lang1(CoreLang) :

def Function(self):
WriteStruct().G enerate()

Then somehow this WriteStruct should magically know that has been
instantiated in Lang1.
But this is not really needed. Just wondered if it was possible.
Ram

May 29 '07 #5
On May 29, 8:52 pm, glomde <tbr...@yahoo.c omwrote:
Hi I wonder if you can set what subclass a class should
have at instance creation.

The problem is that I have something like:

class CoreLang():
def AssignVar(self, var, value):
pass

class Lang1(CoreLang) :
def AssignVar(self, var, value):
return var, "=", value

class Lang2(CoreLang) :
def AssignVar(self, var, value):
return var, "<=", value

class WriteStruct():
def Generate(self, vars):
for var in vars:
print self.AssignVar( )

The problem is that I want WriteStruct to sometimes be a subclass of
Lang1 and sometimes
of Lang2.
In the above example I could but the Generate Method in CoreLang. But
in my real
example I also want to able to subclass WriteStruct to be able to easy
customize WriteStruct.
Which I wouldnt be able to do if it was a method in CoreLang.

So in code I would like to write something like:

WriteStruct(Lan g1).Generate(va rs)
class WriteStruct:
def __init__(self, SubClass):
self._sub = SubClass()
def Generate(self, vars):
for var in vars:
print self._sub.Assig nVar()
Even better would be that if I in the Lang1 class could
just do WriteStruct().G enerate(vars) and Lang1 class would
magically make WriteStruct a subclass of itself.
I don't think I understood what you want here.

Ram

May 29 '07 #6
glomde schrieb:
Hi I wonder if you can set what subclass a class should
have at instance creation.

The problem is that I have something like:

class CoreLang():
def AssignVar(self, var, value):
pass

class Lang1(CoreLang) :
def AssignVar(self, var, value):
return var, "=", value

class Lang2(CoreLang) :
def AssignVar(self, var, value):
return var, "<=", value

class WriteStruct():
def Generate(self, vars):
for var in vars:
print self.AssignVar( )

The problem is that I want WriteStruct to sometimes be a subclass of
Lang1 and sometimes
of Lang2.
In the above example I could but the Generate Method in CoreLang. But
in my real
example I also want to able to subclass WriteStruct to be able to easy
customize WriteStruct.
Which I wouldnt be able to do if it was a method in CoreLang.

So in code I would like to write something like:

WriteStruct(Lan g1).Generate(va rs)

Even better would be that if I in the Lang1 class could
just do WriteStruct().G enerate(vars) and Lang1 class would
magically make WriteStruct a subclass of itself.
Cheers,

/T
If you really need to inherit at runtime, you could utilize `type()`.
>>def foo(self, blah):
.... print self, blah
....
>>attrs = {'foo': foo}
cls = type('MyCls', (object,), attrs)
cls().foo(4 )
<__main__.MyC ls object at 0x009E86D04

Changing ``object`` (the tuple contains all bases) will change the
parent. But, better stick to previous solutions. :)

Stargaming
May 29 '07 #7
glomde wrote:
Hi I wonder if you can set what subclass a class should
have at instance creation.

The problem is that I have something like:

class CoreLang():
def AssignVar(self, var, value):
pass

class Lang1(CoreLang) :
def AssignVar(self, var, value):
return var, "=", value

class Lang2(CoreLang) :
def AssignVar(self, var, value):
return var, "<=", value

class WriteStruct():
def Generate(self, vars):
for var in vars:
print self.AssignVar( )

The problem is that I want WriteStruct to sometimes be a subclass of
Lang1 and sometimes
of Lang2.
In the above example I could but the Generate Method in CoreLang. But
in my real
example I also want to able to subclass WriteStruct to be able to easy
customize WriteStruct.
Which I wouldnt be able to do if it was a method in CoreLang.

So in code I would like to write something like:

WriteStruct(Lan g1).Generate(va rs)

Even better would be that if I in the Lang1 class could
just do WriteStruct().G enerate(vars) and Lang1 class would
magically make WriteStruct a subclass of itself.

You should rethink your program design.

It seems that when you create a WriteStruct you should really be passing
its __init__() method the class that you want it to be a "subclass" of,
creating an instance of that class, and then using generic delegation to
that subclass (using a modified __getattr__()) to handle methods that
aren't found in the WriteStruct.

I can see there are circumstances in which this might not work, but I
believe your current ugly intentions reveal a design smell that you
really need to get rid of if you want a clean program.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogs pot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------
May 29 '07 #8
glomde wrote:
Hi I wonder if you can set what subclass a class should
have at instance creation.

The problem is that I have something like:

class CoreLang():
def AssignVar(self, var, value):
pass

class Lang1(CoreLang) :
def AssignVar(self, var, value):
return var, "=", value

class Lang2(CoreLang) :
def AssignVar(self, var, value):
return var, "<=", value

class WriteStruct():
def Generate(self, vars):
for var in vars:
print self.AssignVar( )

The problem is that I want WriteStruct to sometimes be a subclass of
Lang1 and sometimes
of Lang2.
In the above example I could but the Generate Method in CoreLang. But
in my real
example I also want to able to subclass WriteStruct to be able to easy
customize WriteStruct.
Which I wouldnt be able to do if it was a method in CoreLang.

So in code I would like to write something like:

WriteStruct(Lan g1).Generate(va rs)

Even better would be that if I in the Lang1 class could
just do WriteStruct().G enerate(vars) and Lang1 class would
magically make WriteStruct a subclass of itself.

You should rethink your program design.

It seems that when you create a WriteStruct you should really be passing
its __init__() method the class that you want it to be a "subclass" of,
creating an instance of that class, and then using generic delegation to
that subclass (using a modified __getattr__()) to handle methods that
aren't found in the WriteStruct.

I can see there are circumstances in which this might not work, but I
believe your current ugly intentions reveal a design smell that you
really need to get rid of if you want a clean program.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogs pot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 29 '07 #9
On 29 Maj, 22:45, Steve Holden <s...@holdenweb .comwrote:
glomde wrote:
Hi I wonder if you can set what subclass a class should
have at instance creation.
The problem is that I have something like:
class CoreLang():
def AssignVar(self, var, value):
pass
class Lang1(CoreLang) :
def AssignVar(self, var, value):
return var, "=", value
class Lang2(CoreLang) :
def AssignVar(self, var, value):
return var, "<=", value
class WriteStruct():
def Generate(self, vars):
for var in vars:
print self.AssignVar( )
The problem is that I want WriteStruct to sometimes be a subclass of
Lang1 and sometimes
of Lang2.
In the above example I could but the Generate Method in CoreLang. But
in my real
example I also want to able to subclass WriteStruct to be able to easy
customize WriteStruct.
Which I wouldnt be able to do if it was a method in CoreLang.
So in code I would like to write something like:
WriteStruct(Lan g1).Generate(va rs)
Even better would be that if I in the Lang1 class could
just do WriteStruct().G enerate(vars) and Lang1 class would
magically make WriteStruct a subclass of itself.

You should rethink your program design.

It seems that when you create a WriteStruct you should really be passing
its __init__() method the class that you want it to be a "subclass" of,
creating an instance of that class, and then using generic delegation to
that subclass (using a modified __getattr__()) to handle methods that
aren't found in the WriteStruct.
This is what I am going to do. For some reason I got stuck to think
I needed to solve it with inheritance.

Didnt think of the possibility to modify getattr to make
the delegation be much nicer. Thanks for the tip

>
I can see there are circumstances in which this might not work, but I
believe your current ugly intentions reveal a design smell that you
really need to get rid of if you want a clean program.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogs pot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 29 '07 #10

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

Similar topics

6
3181
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
8
1565
by: Nick | last post by:
I have the following code: var obj = {a:0, b:1, ...} function f() {...} obj.f = f // This will make a instance method? How to make a Class method for the class of obj? Or what's the class of "obj"?
166
8524
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
0
2075
by: Craig Buchanan | last post by:
i am trying to build an application that uses plugins to extend the business functionality of an application. i've decided to use inheritance of abstract classes as the mechanism to do this. moreover, i'm using a factory pattern to control the creation of these classes (see full code below). i'm having a bit of difficulty getting the...
5
3661
by: crystalattice | last post by:
I've finally figured out the basics of OOP; I've created a basic character creation class for my game and it works reasonably well. Now that I'm trying to build a subclass that has methods to determine the rank of a character but I keep getting errors. I want to "redefine" some attributes from the base class so I can use them to...
1
1306
by: aum | last post by:
Hi, For the benefit of folks coming from other languages such as Python, I've created a small framework that simplifies the creation and use of classes, subclasses and instances. http://www.freenet.org.nz/js/classframework.html Features: - freely mix classical and prototypal inheritance
26
5341
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... ...
5
3158
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a instance is created by "calling" a class object.
1
14218
by: johnsonlau | last post by:
When a class derives from a class, You can use a pointer to the parent class to delete the instance of child only when a virtual destructor declared in the parent. class Parent { virtual ~Parent(); // virtual destructor }
0
7478
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...
0
7410
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...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
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...
0
7773
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...
0
5984
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...
0
4960
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...
1
1901
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
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.