<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: metaclass code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 02:59:55 GMT</pubDate>
    <description>DZone Snippets: metaclass code</description>
    <item>
      <title>A dynamic form of the class statement.</title>
      <link>http://snippets.dzone.com/posts/show/1708</link>
      <description>These 2 methods are equivalent.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# method 1&lt;br /&gt;class X(object):&lt;br /&gt;    a = 1&lt;br /&gt;&lt;br /&gt;# method 2&lt;br /&gt;X = type('X', (object,), dict(a=1))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 17 Mar 2006 20:21:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1708</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Another metaclass example</title>
      <link>http://snippets.dzone.com/posts/show/1379</link>
      <description>Taken from this c.l.p. &lt;a href=http://groups.google.com/group/comp.lang.python/browse_frm/thread/4be7f1d3f1b3f7df&gt;thead&lt;/a&gt;&lt;br /&gt;Non-metaclass version&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# using C as parent&lt;br /&gt;class C(object):&lt;br /&gt;     observers = []&lt;br /&gt;&lt;br /&gt;     @classmethod&lt;br /&gt;     def showObservers(cls):&lt;br /&gt;         print cls.observers &lt;br /&gt;&lt;br /&gt;# D1, D2 only inherit class method&lt;br /&gt;class D1(C):&lt;br /&gt;     observers = [] # redundant&lt;br /&gt;&lt;br /&gt;class D2(C):&lt;br /&gt;     observers = [] # redundant &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Metaclass version&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# C uses M as metaclass&lt;br /&gt;class M(type):&lt;br /&gt;     def __init__(cls, name, bases, dict):&lt;br /&gt;         cls.observers = []&lt;br /&gt;&lt;br /&gt;     def showObservers(cls):&lt;br /&gt;         print cls.observers&lt;br /&gt;&lt;br /&gt;class C(object):&lt;br /&gt;     __metaclass__ = M&lt;br /&gt;&lt;br /&gt;# D1, D2 inherit both methods and variables&lt;br /&gt;class D1(C): pass&lt;br /&gt;class D2(C): pass &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Feb 2006 11:16:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1379</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Simple example of meta-class</title>
      <link>http://snippets.dzone.com/posts/show/787</link>
      <description>Use metaclass to define the representation&lt;br /&gt;of a class (not an instance).&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; class Pretty(type):&lt;br /&gt;...     def __str__(cls):&lt;br /&gt;...         return '&lt;Class ' + cls.__name__ +'&gt;'&lt;br /&gt;...     &lt;br /&gt;&gt;&gt;&gt; class Foo(object):&lt;br /&gt;...     __metaclass__ = Pretty&lt;br /&gt;...     &lt;br /&gt;&gt;&gt;&gt; str(Foo)&lt;br /&gt;'&lt;Class Foo&gt;'&lt;br /&gt;&gt;&gt;&gt; &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Representation of an instance can be done using class.&lt;br /&gt;Representation of a class can be done using meta-class.&lt;br /&gt;The code is adapted from this &lt;a href=http://orbtech.com/blog/simplemetaclass&gt;blog post&lt;/a&gt;.</description>
      <pubDate>Thu, 06 Oct 2005 01:07:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/787</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Declarative metaclass</title>
      <link>http://snippets.dzone.com/posts/show/779</link>
      <description>Metaclass is like a black magic in python.&lt;br /&gt;I wouldn't learn it if not necessary to read someone's code.&lt;br /&gt;SqlObject uses it. So, if I want to port it to pys60 Dbms,&lt;br /&gt;I need to learn metaclass.&lt;br /&gt;&lt;br /&gt;Fortunately, this one is not too much to understand.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class DeclarativeMeta(type):&lt;br /&gt;    def __new__(meta, class_name, bases, new_attrs):&lt;br /&gt;        cls = type.__new__(meta, class_name, bases, new_attrs)&lt;br /&gt;        if new_attrs.has_key('__classinit__'):&lt;br /&gt;            cls.__classinit__ = staticmethod(cls.__classinit__.im_func)&lt;br /&gt;        cls.__classinit__(cls, new_attrs)&lt;br /&gt;        return cls&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;When you use it you do this&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class YourClass(object):&lt;br /&gt;    __metaclass__ = DeclarativeMeta&lt;br /&gt;    # ... and other class variables&lt;br /&gt;    def __classinit__(cls, new_attrs):&lt;br /&gt;        # do whatever you want with the new class&lt;br /&gt;        # ...&lt;br /&gt;    def __init__(self):&lt;br /&gt;        # do whatever you want with the new instance&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;You use this when you want a subclass to have some magic&lt;br /&gt;done to it when it is first defined. What you say in&lt;br /&gt;__classinit__ will get done to the new subclass, eg.&lt;br /&gt;create more methods, properties, etc.</description>
      <pubDate>Sat, 01 Oct 2005 19:23:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/779</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
