Metaclass in Python

jieforest發表於2012-07-06
[i=s] 本帖最後由 jieforest 於 2012-7-6 11:05 編輯

In this post we will be talking about Metaclass in python. If you are reading some code which uses metaclass, you will probably come across __new__. If you are not familiar with what __new__ does, i suggest you first read about __new__.

You can read about __new__ in our last to last post.


Throughout this post we will be talking about new style. classes. Things might differ in old-style. classes.

As we go, i will be trying everything on IPython and suggest you as well to try everything on IPython.

Let's see a little bit about normal Objects in Python.

CODE:

>>> class A(object):
...     pass
...We created a class named A. A extends from class object (Here by object we mean class named object from which all new style. classes extend).

Hereafter if we use lowercase 'o' in object, it means we want to refer to the class named object. If we use an Object with uppercase 'O', we mean an instance of some class i.e an instance created by calling the class.

Let's create an instance of A.

CODE:

>>> bj=A()

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-734765/,如需轉載,請註明出處,否則將追究法律責任。

相關文章