I have a function that takes a reference to a class, and then
instantiates that class (and then does several other things with the
new instance). This is easy enough:
item = cls(self, **itemArgs)
where "cls" is the class reference, and itemArgs is obviously a set of
keyword arguments for its __init__ method.
But now I want to generalize this to handle a set of mix-in classes.
Normally you use mixins by creating a class that derives from two or
more other classes, and then instantiate that custom class. But in my
situation, I don't know ahead of time which mixins might be used and
in what combination. So I'd like to take a list of class references,
and instantiate an object that derives from all of them, dynamically.
Is this possible? If so, how?
Thanks,
- Joe 7 3231
Joe Strout <jo*@strout.netwrites:
I have a function that takes a reference to a class, and then
instantiates that class (and then does several other things with the
new instance). This is easy enough:
item = cls(self, **itemArgs)
where "cls" is the class reference, and itemArgs is obviously a set of
keyword arguments for its __init__ method.
But now I want to generalize this to handle a set of mix-in classes.
Normally you use mixins by creating a class that derives from two or
more other classes, and then instantiate that custom class. But in my
situation, I don't know ahead of time which mixins might be used and
in what combination. So I'd like to take a list of class references,
and instantiate an object that derives from all of them, dynamically.
Is this possible? If so, how?
Of course it's possible: use type(name, bases, dict).
>>class A(object): pass
....
>>class B(object): pass
....
>>C = type('C', (A, B), {}) issubclass(C, A)
True
>>issubclass(C, B)
True
Call-by-object'ly yours
--
Arnaud
On Nov 21, 5:11*pm, Joe Strout <j...@strout.netwrote:
I have a function that takes a reference to a class, and then *
instantiates that class (and then does several other things with the *
new instance). *This is easy enough:
* * item = cls(self, **itemArgs)
where "cls" is the class reference, and itemArgs is obviously a set of *
keyword arguments for its __init__ method.
But now I want to generalize this to handle a set of mix-in classes. *
Normally you use mixins by creating a class that derives from two or *
more other classes, and then instantiate that custom class. *But in my *
situation, I don't know ahead of time which mixins might be used and *
in what combination. *So I'd like to take a list of class references, *
and instantiate an object that derives from all of them, dynamically.
Is this possible? *If so, how?
Easily:
derived_cls = type('Derived', (cls1, cls2, *rest_classes), {})
item = derived_cls(**itemArgs)
You will probably want to cache the generated classes so that at most
one class is created for each combination of mixins.
HTH,
George
On Nov 21, 2008, at 3:30 PM, Arnaud Delobelle wrote:
Of course it's possible: use type(name, bases, dict).
Thanks, I never knew about that form of type(). Neither does the
2.5.2 reference manual, whose only index entry for the type() function
is <http://www.python.org/doc/2.5.2/ref/objects.html#l2h-21>, and that
speaks only about the traditional use of type() to check the type of
an object.
help(type) does mention the form you show, though it doesn't explain
what the dict is for.
Where would I find documentation on this nifty function?
Thanks,
- Joe
In article <0E**********************************@strout.net >,
Joe Strout <jo*@strout.netwrote:
On Nov 21, 2008, at 3:30 PM, Arnaud Delobelle wrote:
Of course it's possible: use type(name, bases, dict).
Thanks, I never knew about that form of type(). Neither does the
2.5.2 reference manual, whose only index entry for the type() function
is <http://www.python.org/doc/2.5.2/ref/objects.html#l2h-21>, and that
speaks only about the traditional use of type() to check the type of
an object.
help(type) does mention the form you show, though it doesn't explain
what the dict is for.
Where would I find documentation on this nifty function?
Where built-in functions are documented, the Python Library Reference:
<http://www.python.org/doc/2.5.2/lib/built-in-funcs.html>
--
Ned Deily, na*@acm.org
On Nov 21, 2008, at 6:06 PM, Ned Deily wrote:
>Where would I find documentation on this nifty function?
Where built-in functions are documented, the Python Library Reference:
<http://www.python.org/doc/2.5.2/lib/built-in-funcs.html>
Perfect, thank you. (Odd that the index entry for type() doesn't link
to this page.)
Best,
- Joe
Joe Strout wrote:
On Nov 21, 2008, at 6:06 PM, Ned Deily wrote:
>>Where would I find documentation on this nifty function?
Where built-in functions are documented, the Python Library Reference:
<http://www.python.org/doc/2.5.2/lib/built-in-funcs.html>
Perfect, thank you. (Odd that the index entry for type() doesn't link
to this page.)
Yeah, the indexing isn't perfect. Not sure what to do about that without
filing bugs for each case (which I am as guilty of not doing as anyone
else).
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
On Fri, 21 Nov 2008 15:11:20 -0700, Joe Strout wrote:
I have a function that takes a reference to a class,
Hmmm... how do you do that from Python code? The simplest way I can think
of is to extract the name of the class, and then pass the name as a
reference to the class, and hope it hasn't been renamed in the meantime:
def foo(cls_name, item_args):
# Won't necessarily work for nested scopes.
cls = globals()[cls_name]
item = cls(**itemArgs)
return item
instance = foo(Myclass.__name__, {'a':1})
Seems awfully complicated. If I were you, I'd forget the extra layer of
indirection and just pass the class itself, rather than trying to
generate some sort of reference to it. Let the Python virtual machine
worry about what is the most efficient mechanism to use behind the scenes.
[...]
But now I want to generalize this to handle a set of mix-in classes.
Normally you use mixins by creating a class that derives from two or
more other classes, and then instantiate that custom class. But in my
situation, I don't know ahead of time which mixins might be used and in
what combination. So I'd like to take a list of class references, and
instantiate an object that derives from all of them, dynamically.
Is this possible? If so, how?
It sounds like you need to generate a new class on the fly. Here's one
way:
# untested
def foo(cls, item_args, mixins=None):
superclasses = [cls] + (mixins or [])
class MixedClass(*superclasses):
pass
item = MixedClass(**itemArgs)
return item
instance = foo(MyClass, {'a':1}, [Aclass, Bclass, Cclass])
--
Steven This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Frank Millman |
last post by:
Hi all
I have a question regarding inheritance. I have come up with a
solution, but it is not very elegant - I am sure there is a more
pythonic approach. Assume the following class definitions....
|
by: Kiyomi |
last post by:
Hello,
I create a Table1 dynamically at run time, and at the same time, I would
like to create LinkButton controls, also dynamically, and insert them into
each line in my Table1. I would...
|
by: Tapi |
last post by:
Hello there,
I am new to ASP .NET and am tryimg to create a RadioButtonList dynamically.
My code below gives an "Index was out of range" error.
Where am I going wrong?
|
by: DotNetJunkies User |
last post by:
Hi,
Does anyone know how/if you can instantiate a C# reference type object dynamically? More specifically, my project has a number of classes that I've created and in some cases it would be very...
|
by: Ray |
last post by:
I want to dynamically load DLLs (created from VB) and instantiate a
class with a particular name, like "ProcessClass".
I am able to load the DLL and confirm there is a class by that name BUT
I...
| |
by: sj via .NET 247 |
last post by:
I have a web form
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="test.aspx.vb" Inherits="test"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
...
|
by: Tomas |
last post by:
A newbie question: How can I instantiate objects dynamically in
VB.NET. E.g. I have the object 'Player' and I would like to
instantiate it with the several instances (James, Gunner, etc.),
without...
|
by: Smithers |
last post by:
Using 3.5, I am stuck in attempting to:
1. Dynamically load an assembly
2. Instantiate a class from that assembly (the client code is in a different
namespace than the namespace of the...
|
by: Adam C. |
last post by:
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |