473,557 Members | 2,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Metaclassing

1 New Member
Hi Everyone,
I am originally a Delphi Developer. I'm learning python, and i've pretty much read trough the Python Bible. My issue as the title indicates is metaclassing. In summary what i am trying to do is this:
I have 2 objects - ObjectA and ObjectB.
ObjectA is of type TComplexObject
ObjectB is undefined.
I would like to ask ObjectA for its class information and store it as ClassA.
Then I would like to instantiate ObjectB by using the information in ClassA...
something like - ObjectB = ClassA().

In delphi, it is normally:
ClassA := ObjectA.ClassTy pe
ObjectB := ClassA.Create()

How would i duplicate such functionality in python?
Aug 22 '07 #1
2 1367
bartonc
6,596 Recognized Expert Expert
Hi Everyone,
I am originally a Delphi Developer. I'm learning python, and i've pretty much read trough the Python Bible. My issue as the title indicates is metaclassing. In summary what i am trying to do is this:
I have 2 objects - ObjectA and ObjectB.
ObjectA is of type TComplexObject
ObjectB is undefined.
I would like to ask ObjectA for its class information and store it as ClassA.
Then I would like to instantiate ObjectB by using the information in ClassA...
something like - ObjectB = ClassA().

In delphi, it is normally:
ClassA := ObjectA.ClassTy pe
ObjectB := ClassA.Create()

How would i duplicate such functionality in python?
Expand|Select|Wrap|Line Numbers
  1. >>> class aClass:
  2. ...     classVar1 = 'hello'
  3. ...     def __init__(self, arg1):
  4. ...         self.instVar1 = arg1
  5. ...         
  6. >>> anInstance = aClass('world')
  7. ## store the instance's class
  8. bClass = anInstance.__class__
  9. ## or just create a new instance
  10. >>> anotherInstance = anInstance.__class__('goodby')
  11. >>> 
  12. >>> anotherInstance.classVar1
  13. 'hello'
  14. >>> anotherInstance.instVar1
  15. 'goodby'
  16. >>> anInstance.instVar1
  17. 'world'
  18. >>> 
Aug 25 '07 #2
bartonc
6,596 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. >>> class aClass:
  2. ...     classVar1 = 'hello'
  3. ...     def __init__(self, arg1):
  4. ...         self.instVar1 = arg1
  5. ...         
  6. >>> anInstance = aClass('world')
  7. ## store the instance's class
  8. bClass = anInstance.__class__
  9. ## or just create a new instance
  10. >>> anotherInstance = anInstance.__class__('goodby')
  11. >>> 
  12. >>> anotherInstance.classVar1
  13. 'hello'
  14. >>> anotherInstance.instVar1
  15. 'goodby'
  16. >>> anInstance.instVar1
  17. 'world'
  18. >>> 
Expand|Select|Wrap|Line Numbers
  1. # to avoid the reference demonstrated here:
  2. >>> bClass = aClass
  3. >>> aClass
  4. <class __builtin__.aClass at 0x02FA7240>
  5. >>> bClass
  6. <class __builtin__.aClass at 0x02FA7240>
  7. # use new.classobj()
  8. >>> import new
  9. >>> bClass = new.classobj('bClass', (anInstance.__class__,), anInstance.__class__.__dict__)
  10. >>> bClass
  11. <class __builtin__.bClass at 0x02FA7570>
Aug 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6068
by: delisonews | last post by:
I'm looking for a simple, filesystem-based message board. (No MySQL!) Something that I could include easily in my code: include '../inc/messageboard.php'; .... so that the board shows up at the bottom of every PHP page. The board should have just the basic features, like: - posting capability
3
3668
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example program #include <list>
8
6486
by: Dan | last post by:
Using XML::Simple in perl is extreemly slow to parse big XML files (can be up to 250M, taking ~1h). How can I increase my performance / reduce my memory usage? Is SAX the way forward?
6
2052
by: Manuel Collado | last post by:
I would like to write simple, yet well structured documents with a really simple XML DTD (or schema). Either Docbook or SDocbook are overkill for this simple case. XHTML is simpler, but unstructured (no nested sections). And is not really very simple. Before trying to setup a stripped-down document format by myself, I would like to know...
11
2681
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
13
5713
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was hoping the clc crowd had some ideas for making it even simpler! In-lined below the full program (132 lines). It's a circular singular linked list...
7
2270
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request that page like http://machinename/dir1/hellp.aspx instead of running that page it starts downloding ...whats missing here ....why the aspx engine...
14
2963
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
10
2116
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel <System.Web.Services.WebService(Namespace:="http://...
0
7629
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
7835
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. ...
1
7593
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7914
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...
1
5455
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...
0
5172
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...
0
3602
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...
0
3578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1164
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.