473,320 Members | 2,146 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,320 software developers and data experts.

Simple Metaclassing

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.ClassType
ObjectB := ClassA.Create()

How would i duplicate such functionality in python?
Aug 22 '07 #1
2 1357
bartonc
6,596 Expert 4TB
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.ClassType
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 Expert 4TB
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
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...
3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
8
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
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...
11
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
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...
7
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...
14
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
10
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.