* Not creating any object of class 'Test'.
class MyMetaClass(type):
def __new__(meta, classname, bases, classDict):
print "\n--------MyMetaClass-------__new__-----"
print '\nmeta:---------', meta
print '\nClass Name:---------', classname
print '\nBases:---------', bases
print '\nClass Attributes:---------', classDict
return type.__new__(meta, classname, bases, classDict)
def __init__(cls, *arg):
print "\n--------MyMetaClass-------__init__-----", arg
print "\n--------MyMetaClass-------cls---------", cls
print "\n--------MyMetaClass-------dir(cls)---------", dir(cls)
class BaseCls(object):
def __init__(self, *arg):
print "\n--------BaseCls-------__init__-----", arg
def base_cls_method(self):
print "\n--------BaseCls-------basecls_method-----"
class Test(BaseCls):
__metaclass__ = MyMetaClass
#methods in this class are passed into the metaclass 'MyMetaClass' as functions.
def __init__(self, *arg):
self.number = arg
#member datas in this class are not passed into the metaclass.
print "\n--------Test-------__init__-----", arg
print "\n--------Test-------self---------", self
print "\n--------Test-------dir(self)---------", dir(self)
def test_method(self):
print "\n--------Test-------test_method-----"
OUTPUT
=======
root@saju-laptop:~/Desktop# python aa.py
--------MyMetaClass-------__new__-----
meta:---------
Class Name:--------- Test
Bases:--------- (,)
Class Attributes:--------- {'__module__': '__main__', '__metaclass__': , '__init__': ,
'test_method': }
--------MyMetaClass-------__init__----- ('Test', (,), {'__module__': '__main__',
'__metaclass__': , '__init__': , 'test_method': })
--------MyMetaClass-------cls---------
--------MyMetaClass-------dir(cls)--------- ['__class__', '__delattr__', '__dict__',
'__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__',
'__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'base_cls_method', 'test_method']
=============================== 2
* Creating an object of class 'Test'.
* Here Special class method '__new__' in the metaclass 'MyMetaClass' return an object.
class MyMetaClass(type):
def __new__(meta, classname, bases, classDict):
print "\n--------MyMetaClass-------__new__-----"
print '\nmeta:---------', meta
print '\nClass Name:---------', classname
print '\nBases:---------', bases
print '\nClass Attributes:---------', classDict
return type.__new__(meta, classname, bases, classDict)
def __init__(cls, *arg):
print "\n--------MyMetaClass-------__init__-----", arg
print "\n--------MyMetaClass-------cls---------", cls
print "\n--------MyMetaClass-------dir(cls)---------", dir(cls)
class BaseCls(object):
def __init__(self, *arg):
print "\n--------BaseCls-------__init__-----", arg
def base_cls_method(self):
print "\n--------BaseCls-------basecls_method-----"
class Test(BaseCls):
__metaclass__ = MyMetaClass
#methods in this class are passed into the metaclass 'MyMetaClass' as functions.
def __init__(self, *arg):
self.number = arg
#member datas in this class are not passed into the metaclass.
print "\n--------Test-------__init__-----", arg
print "\n--------Test-------self---------", self
print "\n--------Test-------dir(self)---------", dir(self)
def test_method(self):
print "\n--------Test-------test_method-----"
print "\n-------Test()------",Test(5000)
OUTPUT
========
root@saju-laptop:~/Desktop# python aa.py
--------MyMetaClass-------__new__-----
meta:---------
Class Name:--------- Test
Bases:--------- (,)
Class Attributes:--------- {'__module__': '__main__', '__metaclass__': , '__init__': ,
'test_method': }
--------MyMetaClass-------__init__----- ('Test', (,), {'__module__': '__main__',
'__metaclass__': , '__init__': , 'test_method': })
--------MyMetaClass-------cls---------
--------MyMetaClass-------dir(cls)--------- ['__class__', '__delattr__', '__dict__',
'__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__',
'__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'base_cls_method', 'test_method']
-------Test()------
--------Test-------__init__----- (5000,)
--------Test-------self--------- <__main__.test>
--------Test-------dir(self)--------- ['__class__', '__delattr__', '__dict__',
'__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__',
'__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'base_cls_method', 'number',
'test_method']
<__main__.test>
=============================== 3
* Creating an object of class 'Test'.
* Here Special class method '__new__' in the metaclass 'MyMetaClass' not return anything.
class MyMetaClass(type):
def __new__(meta, classname, bases, classDict):
print "\n--------MyMetaClass-------__new__-----"
print '\nmeta:---------', meta
print '\nClass Name:---------', classname
print '\nBases:---------', bases
print '\nClass Attributes:---------', classDict
def __init__(cls, *arg):
print "\n--------MyMetaClass-------__init__-----", arg
print "\n--------MyMetaClass-------cls---------", cls
print "\n--------MyMetaClass-------dir(cls)---------", dir(cls)
class BaseCls(object):
def __init__(self, *arg):
print "\n--------BaseCls-------__init__-----", arg
def base_cls_method(self):
print "\n--------BaseCls-------basecls_method-----"
class Test(BaseCls):
__metaclass__ = MyMetaClass
#methods in this class are passed into the metaclass 'MyMetaClass' as functions.
def __init__(self, *arg):
self.number = arg
#member datas in this class are not passed into the metaclass.
print "\n--------Test-------__init__-----", arg
print "\n--------Test-------self---------", self
print "\n--------Test-------dir(self)---------", dir(self)
def test_method(self):
print "\n--------Test-------test_method-----"
print "\n-------Test()------",Test(5000)
OUTPUT
========
root@saju-laptop:~/Desktop# python aa.py
--------MyMetaClass-------__new__-----
meta:---------
Class Name:--------- Test
Bases:--------- (,)
Class Attributes:--------- {'__module__': '__main__', '__metaclass__': , '__init__': ,
'test_method': }
-------Test()------
Traceback (most recent call last):
File "aa.py", line 40, in
print "\n-------Test()------",Test(5000)
TypeError: 'NoneType' object is not callable
http://www.voidspace.org.uk/python/articles/metaclasses.shtml
http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python
===============================
No comments:
Post a Comment