Custom Search

Wednesday, January 26, 2011

python django working of object copy technique

python django working of object copy technique

class MyClass:

def __init__(self, name):
self.amount = 100
self.name = name

def test(self):
print "test"


m1 = MyClass('SAJU')

print "\n-------m1---------", m1
print "\n--------dir(m1)----------", dir(m1)
print "\n--------vars(m1)----------", vars(m1)
print "\n---------m1.__dict__-----------------", m1.__dict__
print "\n----------m1.__class__.__name__----------------", m1.__class__.__name__
print "\n===============================================1"


class EmptyClass:
pass

e1 = EmptyClass()

print "\n-------e1---------", e1
print "\n--------dir(e1)----------", dir(e1)
print "\n--------vars(e1)----------", vars(e1)
print "\n---------e1.__dict__-----------------", e1.__dict__
print "\n----------e1.__class__.__name__----------------", e1.__class__.__name__
print "\n-------isinstance(e1, EmptyClass)--------", isinstance(e1, EmptyClass)
print "\n-------isinstance(e1, MyClass)--------", isinstance(e1, MyClass)
print "\n===============================================2"

e1.__class__ = m1.__class__

print "\n-------e1---------", e1
print "\n--------dir(e1)----------", dir(e1)
print "\n--------vars(e1)----------", vars(e1)
print "\n---------e1.__dict__-----------------", e1.__dict__
print "\n----------e1.__class__.__name__----------------", e1.__class__.__name__
print "\n-------isinstance(e1, EmptyClass)--------", isinstance(e1, EmptyClass)
print "\n-------isinstance(e1, MyClass)--------", isinstance(e1, MyClass)
print "\n===============================================3"

e1.__dict__.update(m1.__dict__)

print "\n-------e1---------", e1
print "\n--------dir(e1)----------", dir(e1)
print "\n--------vars(e1)----------", vars(e1)
print "\n---------e1.__dict__-----------------", e1.__dict__
print "\n----------e1.__class__.__name__----------------", e1.__class__.__name__
print "\n-------isinstance(e1, EmptyClass)--------", isinstance(e1, EmptyClass)
print "\n-------isinstance(e1, MyClass)--------", isinstance(e1, MyClass)
print "\n===============================================4"



OUTPUT
=======
-------m1--------- <__main__.myclass>

--------dir(m1)---------- ['__doc__', '__init__', '__module__', 'amount', 'name', 'test']

--------vars(m1)---------- {'amount': 100, 'name': 'SAJU'}

---------m1.__dict__----------------- {'amount': 100, 'name': 'SAJU'}

----------m1.__class__.__name__---------------- MyClass

===============================================1

-------e1--------- <__main__.emptyclass>

--------dir(e1)---------- ['__doc__', '__module__']

--------vars(e1)---------- {}

---------e1.__dict__----------------- {}

----------e1.__class__.__name__---------------- EmptyClass

-------isinstance(e1, EmptyClass)-------- True

-------isinstance(e1, MyClass)-------- False

===============================================2

-------e1--------- <__main__.myclass>

--------dir(e1)---------- ['__doc__', '__init__', '__module__', 'test']

--------vars(e1)---------- {}

---------e1.__dict__----------------- {}

----------e1.__class__.__name__---------------- MyClass

-------isinstance(e1, EmptyClass)-------- False

-------isinstance(e1, MyClass)-------- True

===============================================3

-------e1--------- <__main__.myclass>

--------dir(e1)---------- ['__doc__', '__init__', '__module__', 'amount', 'name', 'test']

--------vars(e1)---------- {'amount': 100, 'name': 'SAJU'}

---------e1.__dict__----------------- {'amount': 100, 'name': 'SAJU'}

----------e1.__class__.__name__---------------- MyClass

-------isinstance(e1, EmptyClass)-------- False

-------isinstance(e1, MyClass)-------- True

===============================================4

No comments:

Post a Comment