Women in Technology

Hear us Roar



Article:
  A Primer on Python Metaclass Programming
Subject:   type not the same as new.classobj
Date:   2007-07-13 16:17:19
From:   hattawayd
In the text you mention: "The class type works as a class factory in the same way that the function new.classobj does"


I'm not sure what the whole different is, but there is a difference.
One example that bit me with Python2.5:


>>> test = new.classobj('test', (), {'a1': 'tA1'})
>>> test.__dict__
{'a1': 'tA1', '__module__': '__main__', '__doc__': None}


>>> test = type('test', (), ('test', (), {'a1': 'tA1'})
>>> test.__dict__
<dictproxy object at 0x00963A70>


So if your code, or the libraries you're using (in my case Django) depend on the value __dict__ then make sure to use the right one.

Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • type not the same as new.classobj
    2007-07-13 17:09:41  David_Mertz [View]

    Python 2.5 made this change, so I obviously didn't know about it when I wrote the article. If you want to assure that you are looking at a real dict, you should be able to wrap the dictproxy object in an idempotent dict() initializer (i.e. no harm in wrapping a real dict that way either)