Custom Search

Friday, December 17, 2010

python django override __new__ special class method tricks

python django override __new__ special class method tricks

class A:
"""
Old style class, Not inheriting from object 'object' (In Python everything is an object).
old style class not call special class method '__new__' when we creating an object of
class 'A'.

Here in '__init__' method we not setting attributes 'salary', 'name' and 'age' to object.
So here we can not get the attributes 'salary', 'name' and 'age' with all the object of
thsi class 'A'.
"""
MY_CLASS_VAR = 1000

def __new__(cls, *args, **kwargs):
print "\n----------__new__----------", cls, args, kwargs

def __init__(self, *args, **kwargs):
print "\n-----------__init__----------"

def test_fun(self):
print "\n----------in test_fun-----------"

a1 = A(200, name='smith', age='25')

print "\n---------a1-----------", a1

print "\n---------dir(a1)------------", dir(a1)

print "\nOld style class, Not inheriting from 'object', No base class 'object'"

print "\n*******************************1"



class A(object):
"""
New style class, inheriting from object 'object' (In Python everything is an object).
Here overrided special classs method '__new__', but it not returing anything.
Here passing our class 'A' and all arguments to special class method '__new__',
when we creating an object of class 'A'.

Here in '__init__' method we not setting attributes 'salary', 'name' and 'age' to object.
So here we can not get the attributes 'salary', 'name' and 'age' with all the object of
thsi class 'A'.
"""
MY_CLASS_VAR = 1000

def __new__(cls, *args, **kwargs):
print "\n----------__new__----------", cls, args, kwargs

def __init__(self, *args, **kwargs):
print "\n-----------__init__-------------"

def test_fun(self):
print "\n----------in test_fun-----------"

a1 = A(200, name='smith', age='25')

print "\n---------a1-----------", a1

print "\n---------dir(a1)------------", dir(a1)

print "\n*******************************2"



class A(object):
"""
New style class, inheriting from object 'object' (In Python everything is an object).
Here overrided special classs method '__new__' and it returing an object by calling
'__new__' method of base class 'object'.
Here passing our class 'A' and all arguments to __new__ method of class 'object',
when we creating an object of class 'A'.

Here in '__init__' method we not setting attributes 'salary', 'name' and 'age' to object.
So here we can not get the attributes 'salary', 'name' and 'age' with all the object of
thsi class 'A'.
"""
MY_CLASS_VAR = 1000

def __new__(cls, *args, **kwargs):
print "\n----------__new__----------", cls, args, kwargs
return object.__new__(cls, *args, **kwargs)

def __init__(self, *args, **kwargs):
print "\n-----------__init__------------"

def test_fun(self):
print "\n----------in test_fun-----------"

a1 = A(200, name='smith', age='25')

print "\n---------a1-----------", a1

print "\n---------dir(a1)------------", dir(a1)

print "\n*******************************3"


class A(object):
"""
New style class, inheriting from object 'object' (In Python everything is an object).
Here overrided special classs method '__new__' and it returing an object by calling
'__new__' method of base class 'object'.
Here passing our class 'A' and all arguments to __new__ method of class 'object',
when we creating an object of class 'A'.

Here in '__init__' method we setting attributes 'salary', 'name' and 'age' to object,
so we can access that attributes values using that object.
So here we get the attributes 'salary', 'name' and 'age' with all the object of thsi
class 'A'.
That is here i can do 'a1.salary', 'a1.name', 'a1.age'.
"""
MY_CLASS_VAR = 1000

def __new__(cls, *args, **kwargs):
print "\n----------__new__----------", cls, args, kwargs
return object.__new__(cls, *args, **kwargs)

def __init__(self, *args, **kwargs):
print "\n-----------__init__------------"
self.salary = args[0]
self.name = kwargs.get('name')
self.age = kwargs.get('age')

def test_fun(self):
print "\n----------in test_fun-----------"

a1 = A(200, name='smith', age='25')

print "\n---------a1-----------", a1

print "\n---------dir(a1)------------", dir(a1)

print "\n*******************************4"

class A(dict):
"""
inheriting from object 'list' (In Python everything is an object).
Here objects of class 'A' will get all the properties amd methods of object 'dict'.

Here in '__init__' method we setting attributes 'salary', 'name' and 'age' to object,
so we can access that attributes values using that object.
So here we get the attributes 'salary', 'name' and 'age' with all the object of thsi
class 'A'.

Here object of class 'A' is a 'dict' object. So i can do dictionary operations like
'len(a1)', 'a1.items()', 'a1.values()' with this object.
Here i can also call and access functions and attributes defined in this class like
'a1.test_fun', 'a1.salary', 'a1..name', 'a1.age'.
"""

MY_CLASS_VAR = 1000

def __new__(cls, *args, **kwargs):
print "\n----------__new__--------------", cls, args, kwargs
return dict.__new__(cls, *args, **kwargs)

def __init__(self, *args, **kwargs):
print "\n------------__init__------------"
self.salary = args[0]
self.name = kwargs.get('name')
self.age = kwargs.get('age')

def test_fun(self):
print "\n----------in test_fun-----------"

a1 = A(200, name='smith', age='25')

print "\n---------a1-----------", a1

print "\n---------dir(a1)------------", dir(a1)

print "\n-------------len(a1)-----------", len(a1)
print "\n-----------a1.items()-------------", a1.items()
print "\n-------------a1.test_fun()-----------", a1.test_fun()
print "\n------------a1.salary------------", a1.salary
print "\n----------a1.name--------------", a1.name
print "\n------------a1.age------------", a1.age

print "\n*******************************5"


class A(list):
"""
inheriting from object 'dict' (In Python everything is an object).
Here objects of class 'A' will get all the properties amd methods of object 'list'.

Here in '__init__' method we setting attributes 'salary', 'name' and 'age' to object,
so we can access that attributes values using that object.
So here we get the attributes 'salary', 'name' and 'age' with all the object of thsi
class 'A'.

Here object of class 'A' is a 'list' object. So i can do dictionary operations like
'len(a1)', 'a1.append()' with this object.
Here i can also call and access functions and attributes defined in this class like
'a1.test_fun', 'a1.salary', 'a1..name', 'a1.age'.
"""

MY_CLASS_VAR = 1000

def __new__(cls, *args, **kwargs):
print "\n----------__new__-----------", cls, args, kwargs
return list.__new__(cls, *args, **kwargs)

def __init__(self, *args, **kwargs):
print "\n-----------__init__----------"
self.salary = args[0]
self.name = kwargs.get('name')
self.age = kwargs.get('age')

def test_fun(self):
print "\n----------in test_fun-----------"

a1 = A(200, name='smith', age='25')

print "\n---------a1-----------", a1

print "\n---------dir(a1)------------", dir(a1)

print "\n-------------len(a1)-----------", len(a1)
print "\n-----------a1.items()-------------", a1.append(4)
print "\n-------------a1.test_fun()-----------", a1.test_fun()
print "\n------------a1.salary------------", a1.salary
print "\n----------a1.name--------------", a1.name
print "\n------------a1.age------------", a1.age

print "\n*******************************6"


OUTPUT
========

# python aa.py

-----------__init__----------

---------a1----------- __main__.A instance at 0x7f3c2226d098>

---------dir(a1)------------ ['MY_CLASS_VAR', '__doc__', '__init__', '__module__', '__new__',
'test_fun']

Old style class, Not inheriting from 'object', No base class 'object'

*******************************1

----------__new__---------- class '__main__.A'> (200,) {'age': '25', 'name': 'smith'}

---------a1----------- None

---------dir(a1)------------ ['__class__', '__delattr__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

*******************************2

----------__new__---------- class '__main__.A'> (200,) {'age': '25', 'name': 'smith'}
aa.py:75: DeprecationWarning: object.__new__() takes no parameters
return object.__new__(cls, *args, **kwargs)

-----------__init__------------

---------a1----------- __main__.A object at 0x7f3c2226f610>

---------dir(a1)------------ ['MY_CLASS_VAR', '__class__', '__delattr__', '__dict__',
'__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__', 'test_fun']

*******************************3

----------__new__---------- class '__main__.A'> (200,) {'age': '25', 'name': 'smith'}
aa.py:106: DeprecationWarning: object.__new__() takes no parameters
return object.__new__(cls, *args, **kwargs)

-----------__init__------------

---------a1----------- __main__.A object at 0x7f3c2226f690>

---------dir(a1)------------ ['MY_CLASS_VAR', '__class__', '__delattr__', '__dict__',
'__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__', 'age', 'name', 'salary', 'test_fun']

*******************************4

----------__new__-------------- class '__main__.A'> (200,) {'age': '25', 'name': 'smith'}

------------__init__------------

---------a1----------- {}

---------dir(a1)------------ ['MY_CLASS_VAR', '__class__', '__cmp__', '__contains__',
'__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',
'__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', 'age', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems',
'iterkeys', 'itervalues', 'keys', 'name', 'pop', 'popitem', 'salary', 'setdefault', 'test_fun',
'update', 'values']

-------------len(a1)----------- 0

-----------a1.items()------------- []

-------------a1.test_fun()-----------
----------in test_fun-----------
None

------------a1.salary------------ 200

----------a1.name-------------- smith

------------a1.age------------ 25

*******************************5

----------__new__----------- class '__main__.A'> (200,) {'age': '25', 'name': 'smith'}

-----------__init__----------

---------a1----------- []

---------dir(a1)------------ ['MY_CLASS_VAR', '__add__', '__class__', '__contains__',
'__delattr__', '__delitem__', '__delslice__', '__dict__', '__doc__', '__eq__', '__format__',
'__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__',
'__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__',
'__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', 'age', 'append', 'count', 'extend', 'index', 'insert', 'name', 'pop', 'remove',
'reverse', 'salary', 'sort', 'test_fun']

-------------len(a1)----------- 0

-----------a1.items()------------- None

-------------a1.test_fun()-----------
----------in test_fun-----------
None

------------a1.salary------------ 200

----------a1.name-------------- smith

------------a1.age------------ 25

*******************************6

No comments:

Post a Comment