Python class representation under the hood

Posted by decentralised on Programmers See other posts from Programmers or by decentralised
Published on 2013-10-24T00:24:41Z Indexed on 2013/10/24 4:07 UTC
Read the original article Hit count: 418

Filed under:
|

OK, here is a simple Python class:

class AddSomething(object):
    __metaclass__ = MyMetaClass
    x = 10
    def __init__(self, a):
        self.a = a

    def add(self, a, b):
        return a + b

We have specified a metaclass, and that means we could write something like this:

class MyMetaClass(type):
    def __init__(cls, name, bases, cdict):
        # do something with the class

Now, the cdict holds a representation of AddSomething:

AddSomething = type('AddSomething', (object,), {'x' : 10, '__init__': __init__, 'add': add})

So my question is simple, are all Python classes represented in this second format internally? If not, how are they represented?

EDIT - Python 2.7

© Programmers or respective owner

Related posts about python

Related posts about meta-programming