By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,825 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,825 IT Pros & Developers. It's quick & easy.

Simple Metaclassing

P: 1
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
Share this Question
Share on Google+
2 Replies

bartonc
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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

Post your reply

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