Custom Search

Sunday, December 19, 2010

Python Django Types and Objects

Python Django Types and Objects

print "\n* object ------> class 'object' "
print "\n* object() -----> creating object of class 'object' "
print "\n-----------object------------", object
print "\n-----------object()------------", object()
print "\n-----------type(object)------------", type(object)
print "\n-----------type(object())------------", type(object())
print "\n-----------object.__class__------------", object.__class__
print "\n-----------object().__class__------------", object().__class__
print "\n-----------object.__bases__------------", object.__bases__
#print "\n-----------object().__bases__---------AttributeError: 'object'
object has no attribute '__bases__'---", object().__bases__


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


print "\n-----------type------------", type
print "\n-----------type(type)------------", type(type)
print "\n-----------type(type(type))------------", type(type(type))
print "\n-----------type.__class__------------", type.__class__
print "\n-----------type(type).__class__------------", type(type).__class__
print "\n-----------type.__bases__------------", type.__bases__


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

print "\n-----------list------------", list
print "\n-----------list()------------", list()
print "\n-----------type(list)------------", type(list)
print "\n-----------type(list())------------", type(list())
print "\n-----------list.__class__------------", list.__class__
print "\n-----------list().__class__------------", list().__class__
print "\n-----------list.__bases__------------", list.__bases__


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


print "\n-----------dict------------", dict
print "\n-----------dict()------------", dict()
print "\n-----------type(dict)------------", type(dict)
print "\n-----------type(dict())------------", type(dict())
print "\n-----------dict.__class__------------", dict.__class__
print "\n-----------dict().__class__------------", dict().__class__
print "\n-----------dict.__bases__------------", dict.__bases__


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


print "\n-----------tuple------------", tuple
print "\n-----------tuple()------------", tuple()
print "\n-----------type(tuple)------------", type(tuple)
print "\n-----------type(tuple())------------", type(tuple())
print "\n-----------tuple.__class__------------", tuple.__class__
print "\n-----------tuple().__class__------------", tuple().__class__
print "\n-----------tuple.__bases__------------", tuple.__bases__


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


print "\n-----------str------------", str
print "\n-----------str()------------", str()
print "\n-----------type(str)------------", type(str)
print "\n-----------type(str())------------", type(str())
print "\n-----------str.__class__------------", str.__class__
print "\n-----------str().__class__------------", str().__class__
print "\n-----------str.__bases__------------", str.__bases__
print "\n-----------basestring.__bases__------------", basestring.__bases__


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


print "\n-----------int------------", int
print "\n-----------int()------------", int()
print "\n-----------type(int)------------", type(int)
print "\n-----------type(int())------------", type(int())
print "\n-----------int.__class__------------", int.__class__
print "\n-----------int().__class__------------", int().__class__
print "\n-----------int.__bases__------------", int.__bases__


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


print "\n* ====Python Built-In Classes====== object, type, list, dict, type, str, int,
float, etc---"
print "\n* ====object of Python Built-In Classes====== object(), type(), list(), dict(),
type(), str(), int(), float(), etc---"


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


print"\n======= creating object of Built-In Classes=========="
print "\n--------[] and list() are same---------------", [], list()
print "\n--------{} and dict() are same---------------", {}, dict()
print "\n--------() and tuple() are same---------------", (), tuple()
print "\n--------'' and str() are same---------------", '', str()
print "\n--------0 and int() are same---------------", 0, int()
print "\n--------0.0 and float() are same---------------", 0.0, float()


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


print"\n======= 'object' is the base or super class of all Built-In Classes=========="
print "\n----No Base class for class 'object'---------object.__bases__------------",
object.__bases__
print "\n-----------type.__bases__------------", type.__bases__
print "\n-----------list.__bases__------------", list.__bases__
print "\n-----------dict.__bases__------------", dict.__bases__
print "\n-----------tuple.__bases__------------", tuple.__bases__
print "\n-----------str.__bases__------------", str.__bases__
print "\n-----------basestring.__bases__------------", basestring.__bases__
print "\n-----------int.__bases__------------", int.__bases__
print "\n-----------float.__bases__------------", float.__bases__


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


print"\n======= type of Built-In Classes are 'type' =========="
print "\n-----------type(object)------------", type(object)
print "\n-----------type(type)------------", type(type)
print "\n-----------type(list)------------", type(list)
print "\n-----------type(dict)------------", type(dict)
print "\n-----------type(tuple)------------", type(tuple)
print "\n-----------type(str)------------", type(str)
print "\n-----------type(int)------------", type(int)
print "\n-----------type(float)------------", type(float)


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


print"\n======= type of Built-In Class object =========="
print "\n-----------type(object())------------", type(object())
print "\n-----------type(type())------------", type(type(type))
print "\n-----------type(list())------------", type(list())
print "\n-----------type(dict())------------", type(dict())
print "\n-----------type(tuple())------------", type(tuple())
print "\n-----------type(str())------------", type(str())
print "\n-----------type(int())------------", type(int())
print "\n-----------type(float())------------", type(float())




OUTPUT
==========


# python aa.py

* object ------> class 'object'

* object() -----> creating object of class 'object'

-----------object------------ type 'object'>

-----------object()------------ object object at 0x7f1502ad0090>

-----------type(object)------------ type 'type'>

-----------type(object())------------ type 'object'>

-----------object.__class__------------ type 'type'>

-----------object().__class__------------ type 'object'>

-----------object.__bases__------------ ()

**************************************************

-----------type------------ type 'type'>

-----------type(type)------------ type 'type'>

-----------type(type(type))------------ type 'type'>

-----------type.__class__------------ type 'type'>

-----------type(type).__class__------------ type 'type'>

-----------type.__bases__------------ (type 'object'>,)

**************************************************

-----------list------------ type 'list'>

-----------list()------------ []

-----------type(list)------------ type 'type'>

-----------type(list())------------ type 'list'>

-----------list.__class__------------ type 'type'>

-----------list().__class__------------ type 'list'>

-----------list.__bases__------------ (type 'object'>,)

**************************************************

-----------dict------------ type 'dict'>

-----------dict()------------ {}

-----------type(dict)------------ type 'type'>

-----------type(dict())------------ type 'dict'>

-----------dict.__class__------------ type 'type'>

-----------dict().__class__------------ type 'dict'>

-----------dict.__bases__------------ (type 'object'>,)

**************************************************

-----------tuple------------ type 'tuple'>

-----------tuple()------------ ()

-----------type(tuple)------------ type 'type'>

-----------type(tuple())------------ type 'tuple'>

-----------tuple.__class__------------ type 'type'>

-----------tuple().__class__------------ type 'tuple'>

-----------tuple.__bases__------------ (type 'object'>,)

**************************************************

-----------str------------ type 'str'>

-----------str()------------

-----------type(str)------------ type 'type'>

-----------type(str())------------ type 'str'>

-----------str.__class__------------ type 'type'>

-----------str().__class__------------ type 'str'>

-----------str.__bases__------------ (type 'basestring'>,)

-----------basestring.__bases__------------ (type 'object'>,)

**************************************************

-----------int------------ type 'int'>

-----------int()------------ 0

-----------type(int)------------ type 'type'>

-----------type(int())------------ type 'int'>

-----------int.__class__------------ type 'type'>

-----------int().__class__------------ type 'int'>

-----------int.__bases__------------ (type 'object'>,)

**************************************************

* ====Python Built-In Classes====== object, type, list, dict, type, str, int, float, etc---

* ====object of Python Built-In Classes====== object(), type(), list(), dict(), type(),
str(), int(), float(), etc---

**************************************************

======= creating object of Built-In Classes==========

--------[] and list() are same--------------- [] []

--------{} and dict() are same--------------- {} {}

--------() and tuple() are same--------------- () ()

--------'' and str() are same---------------

--------0 and int() are same--------------- 0 0

--------0.0 and float() are same--------------- 0.0 0.0

**************************************************

======= 'object' is the base or super class of all Built-In Classes==========

----No Base class for class 'object'---------object.__bases__------------ ()

-----------type.__bases__------------ (type 'object'>,)

-----------list.__bases__------------ (type 'object'>,)

-----------dict.__bases__------------ (type 'object'>,)

-----------tuple.__bases__------------ (type 'object'>,)

-----------str.__bases__------------ (type 'basestring'>,)

-----------basestring.__bases__------------ (type 'object'>,)

-----------int.__bases__------------ (type 'object'>,)

-----------float.__bases__------------ (type 'object'>,)

**************************************************

======= type of Built-In Classes are 'type' ==========

-----------type(object)------------ type 'type'>

-----------type(type)------------ type 'type'>

-----------type(list)------------ type 'type'>

-----------type(dict)------------ type 'type'>

-----------type(tuple)------------ type 'type'>

-----------type(str)------------ type 'type'>

-----------type(int)------------ type 'type'>

-----------type(float)------------ type 'type'>

**************************************************

======= type of Built-In Class object ==========

-----------type(object())------------ type 'object'>

-----------type(type())------------ type 'type'>

-----------type(list())------------ type 'list'>

-----------type(dict())------------ type 'dict'>

-----------type(tuple())------------ type 'tuple'>

-----------type(str())------------ type 'str'>

-----------type(int())------------ type 'int'>

-----------type(float())------------ type 'float'>



Read More....

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

Wednesday, December 15, 2010

Howto Python Django Virtualenv setup

Howto Python Django Virtualenv setup

Virtualenv can download it for free at http://pypi.python.org/pypi/virtualenv.

What virtualenv does is create a new isolated or “virtual” Python environment.
It accom-plishes this by creating a new directory that contains these items:

* A copy of the Python interpreter
* A directory for executable Python scripts
* A site-packages directory for Python modules
* A tool called easy_install that you can use to download and install new Python modules
* Scripts that you can use to “activate” the virtual environment

This Python interpreter will be set up to use the site-packages directory created by
virtualenv rather than the system-wide site-packages directory normally used by Python.
When the virtual environment is active, any Python packages you install (whether via
easy_install, some other tool, or a manual setup.py install) will install into the virtual
environment’s site-packages directory.

* Any libraries you install will be “visible” only to that environment.

* Because different virtual environments don’t interfere with one another,
you can have two projects that use two different modules of the same name—each project
will see only the module that’s installed in its environment.

* If you have two projects that need different versions of a library,
you can simply install the appropriate version in each project’s virtual environment.

1)
Downloaded and install virtualenv.

Downloaded virtualenv choose the source package and use the setup.py script it provides to
install.

OR

#apt-get install python-setuptools <--- This will install the tool 'easy_install'.
#easy_install virtualenv
<------- OR #apt-get install python-virtualenv <------- 2)
To create a new virtual Python environment, open a command line and type:
#python /path/to/virtualenv.py new_environment_name <-------
OR # virtualenv new_environment_name

This will create a new directory called 'new_environment_name',
containing the new Python environment. Inside it is a directory named bin, whose contents
depend on your computer’s operating system:

In LINUX there will be a single script named activate,which is written in thestandard UNIX
bash scripting language.

To run it, simply type "source activate" from a command line in the environment’s bin directory.

To deacti-vate the environment, just close the terminal window or type the command "deactivate".

3)
Once you’ve activated your virtual environment, typing 'python' (
in the same command-line session) will run the virtual environment’s Python interpreter.
The interpreter will then look for Python modules in the virtual environment’s site-packages
directory.

From there you can, for example, install Django by typing:
#easy_install Django <------- This will download the latest Django release package and
install it in the virtual environ-ment’s site-packages directory. You can also place any other
Python modules you’d like in the virtual environment’s site-packages directory, and only
that virtual environment will be able to see them. Creating a new virtual environment each
time you start a project is a good habit to get into, because it’ll greatly simplify the
process of installing and managing Python code.

python django how find class and base classes of an object

python django how find class and base classes of an object

class A:
def __init__(self):
pass

class B(object):
def __init__(self):
pass

class C(A,B):
def __init__(self):
pass

class D(C):
def __init__(self):
pass

c1 = C()
d1 = D()

print "\n-------d1.__class__----------", d1.__class__
print "\n-------d1.__class__.__base__----------", d1.__class__.__base__
print "\n-------d1.__class__.__bases__----------", d1.__class__.__bases__
print "\n-------d1.__class__.__base__.__subclasses__()----------",
d1.__class__.__base__.__subclasses__()

print "\n-------d1.__class__.__mro__----------", d1.__class__.__mro__ #<-------


OUTPUT

=======

# python aa.py

-------d1.__class__---------- class '__main__.D'>

-------d1.__class__.__base__---------- class '__main__.C'>

-------d1.__class__.__bases__---------- (class '__main__.C'>,)

-------d1.__class__.__base__.__subclasses__()---------- [class '__main__.D'>]

-------d1.__class__.__mro__---------- (class '__main__.D'>, class '__main__.C'>,
class __main__.A at 0x7f4c135f0ad0>, class '__main__.B'>, type 'object'>)

python django how find all base classes of a class or object

python django how find base classes of a class

# Using __bases__ and inspect.getmro()

class A:
def __init__(self):
pass

class B(object):
def __init__(self):
pass

class C(A,B):
def __init__(self):
pass

class D(C):
def __init__(self):
pass

c1 = C()
d1 = D()

print "\n--------C.__base__---------", C.__base__
print "\n--------C.__bases__---------", C.__bases__
import inspect
print "\n--------inspect.getmro(C)------", inspect.getmro(C)
print "\n--------C.mro------", C.mro()

print "\n============================="

print "\n--------D.__bases__---------", D.__bases__
print "\n--------inspect.getmro(D)------", inspect.getmro(D)

print "\n============================="

print "\n-------d1.__class__----------", d1.__class__
print "\n-------d1.__class__.__base__----------", d1.__class__.__base__
print "\n-------d1.__class__.__bases__----------", d1.__class__.__bases__
print "\n-------d1.__class__.__base__.__subclasses__()----------",
d1.__class__.__base__.__subclasses__()


OUTPUT
======

# python aa.py

--------C.__base__--------- class '__main__.B'>

--------C.__bases__--------- (class __main__.A at 0x7f78c2434ad0>, class '__main__.B'>)

--------inspect.getmro(C)------ (class '__main__.C'>, class __main__.A at 0x7f78c2434ad0>,
class '__main__.B'>, type 'object'>)

--------C.mro------ [class '__main__.C'>, class __main__.A at 0x7f78c2434ad0>,
class '__main__.B'>, type 'object'>]

=============================

--------D.__bases__--------- (class '__main__.C'>,)

--------inspect.getmro(D)------ (class '__main__.D'>, class '__main__.C'>,
class __main__.A at 0x7f78c2434ad0>, class '__main__.B'>, type 'object'>)

=============================

-------d1.__class__---------- class '__main__.D'>

-------d1.__class__.__base__---------- class '__main__.C'>

-------d1.__class__.__bases__---------- (class '__main__.C'>,)

-------d1.__class__.__base__.__subclasses__()---------- [class '__main__.D'>]

More...
More...


Monday, December 13, 2010

Django Model and Frame class working technique

#Django Model and Frame class working technique.
#Technique of, Django Model classes declaring table columns as class variables.
#Technique of, Django Frame classes declaring fields as class variables.

class MyMetaClass(type):
def __new__(meta, classname, bases, classDict):
print "\n--------MyMetaClass-------__new__-----"
print '\n----meta:---------', meta
print '\n----Class Name:---------', classname
print "\n----Bases:(base classes of class 'Test')---------", bases
print "\n----Class Attributes:(class variables and methods (not member datas)
from class 'Test' )---------", 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):

my_class_var1 = 1000
my_class_var2 = 2000
#all class variables and methods of this class are passed as dictionary in to metaclass
'MyMetaClass'.
#In metaclass these variables and methods are can access in __new__ class special method
from last(3rd) argument.
#Django framework using this technique in Model and Frame classes for create fields.

__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):
#This method is passed into the metaclass 'MyMetaClass' as functions.
print "\n--------Test-------test_method-----"


print "\n-------Test()------",Test(5000)



OUTPUT
======

# python aa.py

--------MyMetaClass-------__new__-----

----meta:---------

----Class Name:--------- Test

----Bases:(base classes of class 'Test')--------- (,)

----Class Attributes:(class variables and methods (not member datas) from class 'Test' )
--------- {'__module__': '__main__', '__metaclass__': , 'test_method': , 'my_class_var2': 2000,
'my_class_var1': 1000, '__init__': }

--------MyMetaClass-------__init__----- ('Test', (,), {'__module__': '__main__',
'__metaclass__': , 'test_method': , 'my_class_var2': 2000, 'my_class_var1': 1000,
'__init__': })

--------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',
'my_class_var1', 'my_class_var2', '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', 'my_class_var1', 'my_class_var2',
'number', 'test_method']
<__main__.test>

--------------------------------------------------------------

Python Django Metaclass working

Python Django Metaclass working

* 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


===============================

Sunday, December 12, 2010

Metaclasses are used to implement Python class objects

Metaclasses are used to implement Python class objects

Because metaclasses implement classes they are normally used to customise the creation (initialisation) of classes.

Class objects are created at runtime. The interpreter takes the end of a class statement as
a signal to call the appropriate metaclass to create the class-object.

If a class is a new style class (inherits from object) the default metaclass is type.
For old style classes the default metaclass is types.ClassType.


To create the class, the metaclass is called with the following arguments :

type(name, bases, dict)

* 'name': The name of the class
* 'bases': A tuple of the base classes of the class
* 'dict': A dictionary of the attributes of the class

This returns the new class.

The following are basically equivalent :

----------------------------a

def __init__(self, x):
self.x = x

def printX(self):
print self.x

Test = type('Test', (object,), {'__init__': __init__, 'printX': printX})


---------------------------b


class Test(object):
def __init__(self, x):
self.x = x

def printX(self):
print self.x



The Most Basic Example:
***********************

So we can create a very simple metaclass which does nothing.

class PointlessMetaClass(type):
def __new__(meta, classname, bases, classDict):
return type.__new__(meta, classname, bases, classDict)

Rather than calling type directly, our metaclass inherits from type, so it calls
type.__new__(meta...). This means that the metaclass returned is an instance of our class.


Using Metaclasses
*****************

You can set the metaclass for a class by setting its __metaclass__ attribute.

We'll adapt the example for above so that we can see what's going on.

class MetaClass(type):
def __new__(meta, classname, bases, classDict):
print 'Class Name:', classname
print 'Bases:', bases
print 'Class Attributes', classDict
return type.__new__(meta, classname, bases, classDict)

class Test(object):
__metaclass__ = MetaClass
def __init__(self):
pass
def method(self):
pass
classAttribute = 'Something'

If you run this you will see something like :

OUTPUT
======

Class Name: Test
Bases: (<type 'object'>,)
Class Attributes {'__module__': '__main__',
'__metaclass__': <class '__main__.MetaClass'>,
'method': <function method at 0x00B412B0>,
'__init__': <function __init__ at 0x00B41070>,
'classAttribute': 'Something'}

* methods on the class are passed into the metaclass as functions.

You can also set the metaclass at the module level. If you set the variable
__metaclass__ in a module, it will be used for all following class definitions that
don't have an explicit metaclasses. New style classes inherit their metaclasses from object,
so that means all old style classes. So you can do things like make all classes into new
style classes by setting __metaclass__ = type.


Metaclasses are used to implement Python class objects (not instance of a class)
********************************************************************************

class Meta(type):
def __str__(self):
return "Klass"

class A(object):
__metaclass__ = Meta

def __str__(self):
return "instance"

print A
print A()

OUTPUT
======

Klass
instance

Good Example, of metaclass implementation is Django models.
http://code.djangoproject.com/browser/django/trunk/django/db/models/base.py

http://www.voidspace.org.uk/python/articles/metaclasses.shtml

===============================

Python Special Class Methods use

Python Special Class Methods use
--------------------------------

In addition to normal class methods, there are a number of special methods that
Python classes can define. Instead of being called directly by your code (like normal methods),
special methods are called for you by Python in particular circumstances or when specific
syntax is used.

http://diveintopython.org/object_oriented_framework/special_class_methods.html
http://diveintopython.org/object_oriented_framework/special_class_methods2.html
http://docs.python.org/reference/datamodel.html#special-method-names

http://stackoverflow.com/questions/2497790/python-overriding-class-not-instance-special-methods

Examples : __new_, __init__, __getattr__, __setattr__, __getattribute__, etc---

----------------------------------------------------------

* Define a class which acts as a sequence (list) and give the elements in reverse order.
* defining __getitem__ on a class make it iterable.
* An object can be iterated over with "for" if it implements __iter__() or __getitem__().


class b:
def __getitem__(self, k):
return k

cb = b()

for k in cb:
print k

OUTPUT
======
1
2
3
.
.
.

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

* Define a class which acts as a sequence (list) and give the elements in reverse order.
* Overriding special class method __len__ and __getitem__.
* Here trying to iterate over object using 'for'.
* Here trying to find length of object using 'len()'.

class Cust_list_cls:
def __init__(self, seq):
print "----in------__init__-----"
self.lst = seq
def __len__(self):
print "\n---in---__len__-------"
return len(self.lst)
def __getitem__(self, i):
print "\n------i-----", i
try:
return self.lst[-(i + 1)]
except Exception, e:
print "exception : ",e
#Iteration will stop when exception occur.
raise e

lt = [2,4,6]

for x in Cust_list_cls(lt):
print x

print "\n------list length-----",len(Cust_list_cls(lt))


OUTPUT
======

----in------__init__-----

------i----- 0
6

------i----- 1
4

------i----- 2
2

------i----- 3
exception : list index out of range

------list length----- ----in------__init__-----

---in---__len__-------
3

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

* Define a class which acts as a sequence (list) and give the elements in reverse order.
* Overriding special class method __len__.
* Not overriding special class method __getitem__.
* Here trying to iterate over object using 'for'.
* Here trying to find length of object using 'len()'.

class Cust_list_cls:
def __init__(self, seq):
print "\n----in------__init__-----"
self.lst = seq
def __len__(self):
print "\n---in---__len__-------"
return len(self.lst)


lt = [2,4,6]

print "\n------list length-----",len(Cust_list_cls(lt))

for x in Cust_list_cls(lt):
print x

OUTPUT
======

------list length-----
----in------__init__-----

---in---__len__-------
3

----in------__init__-----
Traceback (most recent call last):
File "aa.py", line 41, in
for x in Cust_list_cls(lt):
TypeError: iteration over non-sequence

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

* Define a class which acts as a sequence (list) and give the elements in reverse order.
* Overriding special class method __getitem__.
* Not overriding special class method __len__.
* Here trying to iterate over object using 'for'.
* Here trying to find length of object using 'len()'.

class Cust_list_cls:
def __init__(self, seq):
print "\n----in------__init__-----"
self.lst = seq

def __getitem__(self, i):
print "\n------i-----", i
#Iteration will stop when exception occur.
return self.lst[-(i + 1)]

lt = [2,4,6]

for x in Cust_list_cls(lt):
print x

print "\n------list length-----",len(Cust_list_cls(lt))


OUTPUT
=======

----in------__init__-----

------i----- 0
6

------i----- 1
4

------i----- 2
2

------i----- 3

------list length-----
----in------__init__-----
Traceback (most recent call last):
File "aa.py", line 42, in
print "\n------list length-----",len(Cust_list_cls(lt))
AttributeError: Cust_list_cls instance has no attribute '__len__'

=====================================

Python static class data and static class methods

Python Django static class data and static class methods

Static Data
========
simply define a class attribute. To assign a new value to the attribute, you have to
explicitly use the class name in the assignment:

class C:
count = 0 # number of times C.__init__ called

def __init__(self):
C.count = C.count + 1

def getcount(self):
return C.count # or return self.count

c.count also refers to C.count for any c such that isinstance(c, C) holds, unless
overridden by c itself or by some class on the base-class search path from c.__class__
back to C.

Caution:
-----------
within a method of C, an assignment like self.count = 42 creates a new and unrelated
instance vrbl named "count" in self's own dict. Rebinding of a class-static data name
must
always specify the class whether inside a method or not:

* Use class name to access class-static data, inside a method or not:

C.count = 314


Static methods
===========
Static methods are possible when you're using new-style classes:

class C:
def static(arg1, arg2, arg3):
# No 'self' parameter!
...
static = staticmethod(static)

However, a far more straightforward way to get the effect of a static method is via a
simple module-level function:

def getcount():
return C.count

If your code is structured so as to define one class (or tightly related class hierarchy)
per module, this supplies the desired encapsulation.

Saturday, December 11, 2010

python django remove trailing newline from string

python django remove trailing newline from string

fileName = "some text\n"

fileName = fileName.rstrip("\n")
This will remove more than one newline if present.

If you only want to remove one newline, use

if fileName[-1:] == '\n':
fileName = fileName[:-1]

fileName[:-1] is all of the string except for its last character, which is useful for
removing the trailing newline from a string.

* because list exclude the last element.

fileName[-1:] ------> get last element.
fileName[:-1] -------? get all elements except last one.

-------------------------------------------------

>>> s = ' some text \t\n'
>>> s.strip()

'some text'
>>> s.rstrip()

' some text'
>>> s.rstrip('\n')

' some text \t'

How make higher order function Python django


How make higher order function in Python django

You have two choices: you can use nested scopes or you can use callable objects.

For example, suppose you wanted to define linear(a,b) which returns a function f(x)

that computes the value a*x+b. Using nested scopes:

def linear(a,b):
def result(x):
return a*x + b
return result

Or using a callable object:

class linear:
def __init__(self, a, b):
self.a, self.b = a,b
def __call__(self, x):
return self.a * x + self.b

In both cases:

taxes = linear(0.3,2)

gives a callable object where taxes(10e6) == 0.3 * 10e6 + 2.

The callable object approach has the disadvantage that it is a bit slower and results

in slightly longer code. However, note that a collection of callables can share their

signature via inheritance:

class exponential(linear):
# __init__ inherited
def __call__(self, x):
return self.a * (x ** self.b)

Object can encapsulate state for several methods:

class counter:
value = 0
def set(self, x): self.value = x
def up(self): self.value=self.value+1
def down(self): self.value=self.value-1

count = counter()
inc, dec, reset = count.up, count.down, count.set


Here up(), down() and set() act like functions which share the same counting variable.


Thursday, December 9, 2010

Python Child and Super Class with class variables

Python Child and Super Class with class variables

#Python Child and Super Class with class variables

class Base_Class:
int_class_var_in_base_cls = 100
list_class_var_in_base_cls = [10]

def __init__(self):
print "-----Base_Class------__init__----->>>, ", self
self.name = 'saju'

def function_in_base_and_child_class_1_and_2(self):
print "-----Base_Class------function_in_base_and_child_class_1_and_2----self----->>>,
", self
#raise Exception "need to implement in child class"

def test_function_in_base_class(self): #<----------- Note this method.
print "-----Base_Class------test_function_in_base_class-----self----->>>, ", self
self.function_in_base_and_child_class_1_and_2()


class Child_Class_1(Base_Class):

def __init__(self):
print "-----Child_Class_1------__init__----->>>, ", self
self.age=25

def function_in_child_class_1_only(self):
print "-----Child_Class_1------function_in_child_class_1_only----self---->>>, ", self

def function_in_base_and_child_class_1_and_2(self):
print "-----Child_Class_1------function_in_base_and_child_class_1_and_2----self---->>>,
", self


class Child_Class_2(Child_Class_1):

def __init__(self):
print "-----Child_Class_2------__init__----->>>, ", self
self.salary=5000

def function_in_child_class_2_only(self):
print "-----Child_Class_2------function_in_child_class_2_only----self---->>>, ", self

def function_in_base_and_child_class_1_and_2(self):
print "-----Child_Class_2------function_in_base_and_child_class_1_and_2----self---->>>,
", self

print "\n=============== 1 ===============\n"

print "......c2 = Child_Class_2()......"
c2 = Child_Class_2()
print "......c2.test_function_in_base_class()......."
c2.test_function_in_base_class()
print "......c2.function_in_child_class_1_only()......."
c2.function_in_child_class_1_only()
print "......c2.function_in_child_class_2_only()......."
c2.function_in_child_class_2_only()
print "......c2.function_in_base_and_child_class_1_and_2()......."
c2.function_in_base_and_child_class_1_and_2()

print "-----c2.int_class_var_in_base_cls---------", c2.int_class_var_in_base_cls
print "-----c2.list_class_var_in_base_cls---------", c2.list_class_var_in_base_cls
print "-----Base_Class.int_class_var_in_base_cls---------",
Base_Class.int_class_var_in_base_cls
print "-----Base_Class.list_class_var_in_base_cls---------",
Base_Class.list_class_var_in_base_cls
print "-----Child_Class_1.int_class_var_in_base_cls---------",
Child_Class_1.int_class_var_in_base_cls
print "-----Child_Class_1.list_class_var_in_base_cls---------",
Child_Class_1.list_class_var_in_base_cls
print "-----Child_Class_2.int_class_var_in_base_cls---------",
Child_Class_2.int_class_var_in_base_cls
print "-----Child_Class_2.list_class_var_in_base_cls---------",
Child_Class_2.list_class_var_in_base_cls

print "\nPrint instance variables, class variables and methods associated with this object"
print "----------dir(c2)---------------", dir(c2)
print "\nPrint instance variables with value associated with this object"
print "----------vars(c2)---------------", vars(c2)

print "\n=============== 2 ===============\n"

print "-----c2.int_class_var_in_base_cls = 2400---------"
c2.int_class_var_in_base_cls = 2400
print "-----c2.list_class_var_in_base_cls = [560]---------"
c2.list_class_var_in_base_cls = [560]

print "\n----Assigning value to a variable(class variable) using 'self.' or
object should create new instance or member variable of \
that name (class variable name) for that object -----\n"

print "\n----Appending values to a lis type class variable and adding new key value
pair to a dict type class variable should not \
create new instance or member variable----\n"

print "\n----After this we can not access class variables 'int_class_var_in_base_cls'
and 'list_class_var_in_base_cls' using \
'self.' or object, because now that object have the instance or member variables of
name 'int_class_var_in_base_cls' and 'list_class_var_in_base_cls'.\
If we trying to access a variable using 'self.' or object, it first search in object
scope for that variable, then in class scope, then in global scope, then in module
level.----\n"

print "-----c2.int_class_var_in_base_cls---------", c2.int_class_var_in_base_cls
print "-----c2.list_class_var_in_base_cls---------", c2.list_class_var_in_base_cls
print "-----Base_Class.int_class_var_in_base_cls---------",
Base_Class.int_class_var_in_base_cls
print "-----Base_Class.list_class_var_in_base_cls---------",
Base_Class.list_class_var_in_base_cls
print "-----Child_Class_1.int_class_var_in_base_cls---------",
Child_Class_1.int_class_var_in_base_cls
print "-----Child_Class_1.list_class_var_in_base_cls---------",
Child_Class_1.list_class_var_in_base_cls
print "-----Child_Class_2.int_class_var_in_base_cls---------",
Child_Class_2.int_class_var_in_base_cls
print "-----Child_Class_2.list_class_var_in_base_cls---------",
Child_Class_2.list_class_var_in_base_cls

print "\nPrint instance variables, class variables and methods associated with this object"
print "----------dir(c2)---------------", dir(c2)
print "\nPrint instance variables with value associated with this object"
print "----------vars(c2)---------------", vars(c2)

print "\n=============== 3 ===============\n"

print "-----Base_Class.int_class_var_in_base_cls = 1000---------"
Base_Class.int_class_var_in_base_cls = 1000
print "-----Base_Class.list_class_var_in_base_cls = [600]---------"
Base_Class.list_class_var_in_base_cls = [600]

print "-----c2.int_class_var_in_base_cls---------", c2.int_class_var_in_base_cls
print "-----c2.list_class_var_in_base_cls---------", c2.list_class_var_in_base_cls
print "-----Base_Class.int_class_var_in_base_cls---------",
Base_Class.int_class_var_in_base_cls
print "-----Base_Class.list_class_var_in_base_cls---------",
Base_Class.list_class_var_in_base_cls
print "-----Child_Class_1.int_class_var_in_base_cls---------",
Child_Class_1.int_class_var_in_base_cls
print "-----Child_Class_1.list_class_var_in_base_cls---------",
Child_Class_1.list_class_var_in_base_cls
print "-----Child_Class_2.int_class_var_in_base_cls---------",
Child_Class_2.int_class_var_in_base_cls
print "-----Child_Class_2.list_class_var_in_base_cls---------",
Child_Class_2.list_class_var_in_base_cls

print "\nPrint instance variables, class variables and methods associated with this object"
print "----------dir(c2)---------------", dir(c2)
print "\nPrint instance variables with value associated with this object"
print "----------vars(c2)---------------", vars(c2)

print "\n=============== 4 ===============\n"

print "-----Child_Class_1.int_class_var_in_base_cls = 2000---------"
Child_Class_1.int_class_var_in_base_cls = 2000
print "-----Child_Class_1.list_class_var_in_base_cls = [700]---------"
Child_Class_1.list_class_var_in_base_cls = [700]

print "-----c2.int_class_var_in_base_cls---------", c2.int_class_var_in_base_cls
print "-----c2.list_class_var_in_base_cls---------", c2.list_class_var_in_base_cls
print "-----Base_Class.int_class_var_in_base_cls---------",
Base_Class.int_class_var_in_base_cls
print "-----Base_Class.list_class_var_in_base_cls---------",
Base_Class.list_class_var_in_base_cls
print "-----Child_Class_1.int_class_var_in_base_cls---------",
Child_Class_1.int_class_var_in_base_cls
print "-----Child_Class_1.list_class_var_in_base_cls---------",
Child_Class_1.list_class_var_in_base_cls
print "-----Child_Class_2.int_class_var_in_base_cls---------",
Child_Class_2.int_class_var_in_base_cls
print "-----Child_Class_2.list_class_var_in_base_cls---------",
Child_Class_2.list_class_var_in_base_cls

print "\nPrint instance variables, class variables and methods associated with this object"
print "----------dir(c2)---------------", dir(c2)
print "\nPrint instance variables with value associated with this object"
print "----------vars(c2)---------------", vars(c2)

print "\n=============== 5 ===============\n"

print "-----Child_Class_2.int_class_var_in_base_cls = 3000---------"
Child_Class_2.int_class_var_in_base_cls = 3000
print "-----Child_Class_2.list_class_var_in_base_cls = [800]---------"
Child_Class_2.list_class_var_in_base_cls = [800]

print "-----c2.int_class_var_in_base_cls---------", c2.int_class_var_in_base_cls
print "-----c2.list_class_var_in_base_cls---------", c2.list_class_var_in_base_cls
print "-----Base_Class.int_class_var_in_base_cls---------",
Base_Class.int_class_var_in_base_cls
print "-----Base_Class.list_class_var_in_base_cls---------",
Base_Class.list_class_var_in_base_cls
print "-----Child_Class_1.int_class_var_in_base_cls---------",
Child_Class_1.int_class_var_in_base_cls
print "-----Child_Class_1.list_class_var_in_base_cls---------",
Child_Class_1.list_class_var_in_base_cls
print "-----Child_Class_2.int_class_var_in_base_cls---------",
Child_Class_2.int_class_var_in_base_cls
print "-----Child_Class_2.list_class_var_in_base_cls---------",
Child_Class_2.list_class_var_in_base_cls

print "\nPrint instance variables, class variables and methods associated with this object"
print "----------dir(c2)---------------", dir(c2)
print "\nPrint instance variables with value associated with this object"
print "----------vars(c2)---------------", vars(c2)

print "\n=============== 6 ===============\n"


OUTPUT
=======


# python base_class.py

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

......c2 = Child_Class_2()......
-----Child_Class_2------__init__----->>>, <__main__.child_class_2>
......c2.test_function_in_base_class().......
-----Base_Class------test_function_in_base_class-----self----->>>,
<__main__.child_class_2>
-----Child_Class_2------function_in_base_and_child_class_1_and_2----self---->>>,
<__main__.child_class_2>
......c2.function_in_child_class_1_only().......
-----Child_Class_1------function_in_child_class_1_only----self---->>>,
<__main__.child_class_2>
......c2.function_in_child_class_2_only().......
-----Child_Class_2------function_in_child_class_2_only----self---->>>,
<__main__.child_class_2>
......c2.function_in_base_and_child_class_1_and_2().......
-----Child_Class_2------function_in_base_and_child_class_1_and_2----self---->>>,
<__main__.child_class_2>
-----c2.int_class_var_in_base_cls--------- 100
-----c2.list_class_var_in_base_cls--------- [10]
-----Base_Class.int_class_var_in_base_cls--------- 100
-----Base_Class.list_class_var_in_base_cls--------- [10]
-----Child_Class_1.int_class_var_in_base_cls--------- 100
-----Child_Class_1.list_class_var_in_base_cls--------- [10]
-----Child_Class_2.int_class_var_in_base_cls--------- 100
-----Child_Class_2.list_class_var_in_base_cls--------- [10]

Print instance variables, class variables and methods associated with this object
----------dir(c2)--------------- ['__doc__', '__init__', '__module__',
'function_in_base_and_child_class_1_and_2', 'function_in_child_class_1_only',
'function_in_child_class_2_only', 'int_class_var_in_base_cls',
'list_class_var_in_base_cls', 'salary', 'test_function_in_base_class']

Print instance variables with value associated with this object
----------vars(c2)--------------- {'salary': 5000}

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

-----c2.int_class_var_in_base_cls = 2400---------
-----c2.list_class_var_in_base_cls = [560]---------

----Assigning value to a variable(class variable) using 'self.' or object should
create new instance or member variable of that name (class variable name) for
that object -----


----Appending values to a lis type class variable and adding new key value pair to a
dict type class variable should not create new instance or member variable----


----After this we can not access class variables 'int_class_var_in_base_cls' and
'list_class_var_in_base_cls' using 'self.' or object, because now that object have
the instance or member variables of name 'int_class_var_in_base_cls' and
'list_class_var_in_base_cls'.If we trying to access a variable using 'self.'
or object, it first search in object scope for that variable, then in class scope,
then in global scope, then in module level.----

-----c2.int_class_var_in_base_cls--------- 2400
-----c2.list_class_var_in_base_cls--------- [560]
-----Base_Class.int_class_var_in_base_cls--------- 100
-----Base_Class.list_class_var_in_base_cls--------- [10]
-----Child_Class_1.int_class_var_in_base_cls--------- 100
-----Child_Class_1.list_class_var_in_base_cls--------- [10]
-----Child_Class_2.int_class_var_in_base_cls--------- 100
-----Child_Class_2.list_class_var_in_base_cls--------- [10]

Print instance variables, class variables and methods associated with this object
----------dir(c2)--------------- ['__doc__', '__init__', '__module__',
'function_in_base_and_child_class_1_and_2', 'function_in_child_class_1_only',
'function_in_child_class_2_only', 'int_class_var_in_base_cls',
'list_class_var_in_base_cls', 'salary', 'test_function_in_base_class']

Print instance variables with value associated with this object
----------vars(c2)--------------- {'salary': 5000, 'int_class_var_in_base_cls': 2400,
'list_class_var_in_base_cls': [560]}

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

-----Base_Class.int_class_var_in_base_cls = 1000---------
-----Base_Class.list_class_var_in_base_cls = [600]---------
-----c2.int_class_var_in_base_cls--------- 2400
-----c2.list_class_var_in_base_cls--------- [560]
-----Base_Class.int_class_var_in_base_cls--------- 1000
-----Base_Class.list_class_var_in_base_cls--------- [600]
-----Child_Class_1.int_class_var_in_base_cls--------- 1000
-----Child_Class_1.list_class_var_in_base_cls--------- [600]
-----Child_Class_2.int_class_var_in_base_cls--------- 1000
-----Child_Class_2.list_class_var_in_base_cls--------- [600]

Print instance variables, class variables and methods associated with this object
----------dir(c2)--------------- ['__doc__', '__init__', '__module__',
'function_in_base_and_child_class_1_and_2', 'function_in_child_class_1_only',
'function_in_child_class_2_only', 'int_class_var_in_base_cls',
'list_class_var_in_base_cls', 'salary', 'test_function_in_base_class']

Print instance variables with value associated with this object
----------vars(c2)--------------- {'salary': 5000, 'int_class_var_in_base_cls': 2400,
'list_class_var_in_base_cls': [560]}

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

-----Child_Class_1.int_class_var_in_base_cls = 2000---------
-----Child_Class_1.list_class_var_in_base_cls = [700]---------
-----c2.int_class_var_in_base_cls--------- 2400
-----c2.list_class_var_in_base_cls--------- [560]
-----Base_Class.int_class_var_in_base_cls--------- 1000
-----Base_Class.list_class_var_in_base_cls--------- [600]
-----Child_Class_1.int_class_var_in_base_cls--------- 2000
-----Child_Class_1.list_class_var_in_base_cls--------- [700]
-----Child_Class_2.int_class_var_in_base_cls--------- 2000
-----Child_Class_2.list_class_var_in_base_cls--------- [700]

Print instance variables, class variables and methods associated with this object
----------dir(c2)--------------- ['__doc__', '__init__', '__module__',
'function_in_base_and_child_class_1_and_2', 'function_in_child_class_1_only',
'function_in_child_class_2_only', 'int_class_var_in_base_cls',
'list_class_var_in_base_cls', 'salary', 'test_function_in_base_class']

Print instance variables with value associated with this object
----------vars(c2)--------------- {'salary': 5000, 'int_class_var_in_base_cls': 2400,
'list_class_var_in_base_cls': [560]}

=============== 5 ===============

-----Child_Class_2.int_class_var_in_base_cls = 3000---------
-----Child_Class_2.list_class_var_in_base_cls = [800]---------
-----c2.int_class_var_in_base_cls--------- 2400
-----c2.list_class_var_in_base_cls--------- [560]
-----Base_Class.int_class_var_in_base_cls--------- 1000
-----Base_Class.list_class_var_in_base_cls--------- [600]
-----Child_Class_1.int_class_var_in_base_cls--------- 2000
-----Child_Class_1.list_class_var_in_base_cls--------- [700]
-----Child_Class_2.int_class_var_in_base_cls--------- 3000
-----Child_Class_2.list_class_var_in_base_cls--------- [800]

Print instance variables, class variables and methods associated with this object
----------dir(c2)--------------- ['__doc__', '__init__', '__module__',
'function_in_base_and_child_class_1_and_2', 'function_in_child_class_1_only',
'function_in_child_class_2_only', 'int_class_var_in_base_cls',
'list_class_var_in_base_cls', 'salary', 'test_function_in_base_class']

Print instance variables with value associated with this object
----------vars(c2)--------------- {'salary': 5000, 'int_class_var_in_base_cls': 2400,
'list_class_var_in_base_cls': [560]}

=============== 6 ===============

Wednesday, December 8, 2010

Points to remember while Assigning and accessing class variables using 'self.' or object and class name in Python

Points to remember while Assigning and accessing class variables using 'self.' or object and class name in Python


class E:
"""
Points to remember while Assigning and accessing class variables using 'self.' or
object and class name.
"""
class_var_list = []
class_var_num = 100
class_var_str = 'SA-1'
class_var_dict = {}

def __init__(self):
self.name = 'saju'

def create_new_instance_variable_new_var_1(self, value):
#We can create an instance variable by assigne a value to a variable using 'self.'.
self.new_var_1 = value

def print_all_instance_vars(self):
return vars(self)

def get_value_from_new_var_1(self, new_var_1):
return self.new_var_1

def assigne_value_to_class_var_num_using_self(self, value):
#this create an instance variable, because assignment using 'self.'.
self.class_var_num = value

def assigne_value_to_class_var_num_using_class_name(self, value):
#equal to E.class_var_num = value
self.__class__.class_var_num = value

def assigne_value_to_class_var_str_using_self(self, value):
#this create an instance variable, because assignment using 'self.'.
self.class_var_str = value

def assigne_value_to_class_var_str_using_class_name(self, value):
self.__class__.class_var_str = value

def assigne_value_to_class_var_list_using_self(self, value):
#Here appending value to 'class_var_list' using 'self.'
#So it does not create new instance variable 'self.class_var_list'.
#Here 'self.class_var_list' point to class variable 'E.class_var_list', not to
'self.class_var_list'.
#Here not assigning new list to 'self.class_var_list'.
self.class_var_list.append(value)

def assigne_value_to_class_var_list_using_class_name(self, value):
#appending value to class variable 'E.class_var_list'
self.__class__.class_var_list.append(value)

def assigne_value_to_class_var_dict_using_self(self, key, value):
#Here 'self.class_var_dict' point to class variable 'E.class_var_dict', not to
'self.class_var_dict'.
#Here not assigning new dict to 'self.class_var_dict'.
#So it does not create new instance variable 'self.class_var_dict'.
self.class_var_dict[key] = value

def assigne_value_to_class_var_dict_using_class_name(self, key, value):
self.__class__.class_var_dict[key] = value

def get_value_from_class_var_num_using_self(self):
return self.class_var_num

def get_value_from_class_var_num_using_class_name(self):
return self.__class__.class_var_num

def get_value_from_class_var_str_using_self(self):
return self.class_var_str

def get_value_from_class_var_str_using_class_name(self):
return self.__class__.class_var_str

def get_value_from_class_var_list_using_self(self):
return self.class_var_list

def get_value_from_class_var_list_using_class_name(self):
return self.__class__.class_var_list

def get_value_from_class_var_dict_using_self(self):
return self.class_var_dict

def get_value_from_class_var_dict_using_class_name(self):
return self.__class__.class_var_dict

def reset_class_var_num_using_self(self):
#this create an instance variable, because assignment using 'self.'.
self.class_var_num = 800

def reset_class_var_str_using_self(self):
#this create an instance variable, because assignment using 'self.'.
self.class_var_str = 'SA-2'

def reset_class_var_list_using_self(self):
#this create an instance variable, because assignment using 'self.'.
self.class_var_list = []

def reset_class_var_dict_using_self(self):
#this create an instance variable, because assignment using 'self.'.
self.class_var_dict = {}

def reset_class_var_num_using_class_name(self):
self.__class__.class_var_num = 800

def reset_class_var_str_using_class_name(self):
self.__class__.class_var_str = 'SA-2'

def reset_class_var_list_using_class_name(self):
self.__class__.class_var_list = []

def reset_class_var_dict_using_class_name(self):
#equal to E.class_var_dict = {}
self.__class__.class_var_dict = {}

def get_all_local_vars(self):
return locals()

def get_all_global_vars():
return globals()


e1 = E()
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----e1.create_new_instance_variable_new_var_1(25)----",
e1.create_new_instance_variable_new_var_1(25)
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e1.assigne_value_to_class_var_num_using_self(100)----",
e1.assigne_value_to_class_var_num_using_self(100)
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e1.assigne_value_to_class_var_str_using_self(200)----",
e1.assigne_value_to_class_var_str_using_self(200)
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e1.assigne_value_to_class_var_list_using_self(300)----",
e1.assigne_value_to_class_var_list_using_self(300)
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e1.assigne_value_to_class_var_dict_using_self(400)----",
e1.assigne_value_to_class_var_dict_using_self('mark', 400)
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict

print "\n\n--------------------------------------------------------------------"

e2 = E()
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars()
print "----e2.create_new_instance_variable_new_var_1(25)----",
e2.create_new_instance_variable_new_var_1(25)
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e2.assigne_value_to_class_var_num_using_class_name(100)----",
e2.assigne_value_to_class_var_num_using_class_name(100)
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e2.assigne_value_to_class_var_str_using_class_name(200)----",
e2.assigne_value_to_class_var_str_using_class_name(200)
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e2.assigne_value_to_class_var_list_using_class_name(300)----",
e2.assigne_value_to_class_var_list_using_class_name(300)
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e2.assigne_value_to_class_var_dict_using_class_name(400)----",
e2.assigne_value_to_class_var_dict_using_class_name('place', 'Banagalore')
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict

print "\n\n--------------------------------------------------------------------"

e3 = E()
print "----e3.print_all_instance_vars()----", e3.print_all_instance_vars()
print "----e3.create_new_instance_variable_new_var_1(25)----",
e3.create_new_instance_variable_new_var_1(25)
print "----e3.print_all_instance_vars()----", e3.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e3.reset_class_var_num_using_self()----", e3.reset_class_var_num_using_self()
print "----e3.reset_class_var_str_using_self()----", e3.reset_class_var_str_using_self()
print "----e3.reset_class_var_list_using_self()----", e3.reset_class_var_list_using_self()
print "----e3.reset_class_var_dict_using_self()----", e3.reset_class_var_dict_using_self()
print "----e3.print_all_instance_vars()----", e3.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e3.reset_class_var_num_using_class_name()----",
e3.reset_class_var_num_using_class_name()
print "----e3.reset_class_var_str_using_class_name()----",
e3.reset_class_var_str_using_class_name()
print "----e3.reset_class_var_list_using_class_name()----",
e3.reset_class_var_list_using_class_name()
print "----e3.reset_class_var_dict_using_class_name()----",
e3.reset_class_var_dict_using_class_name()
print "----e3.print_all_instance_vars()----", e3.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict


OUTPUT
=======

----e1.print_all_instance_vars()---- {'name': 'saju'}
----e1.create_new_instance_variable_new_var_1(25)---- None
----e1.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name---- 100 SA-1 [] {}
----e1.assigne_value_to_class_var_num_using_self(100)---- None
----e1.print_all_instance_vars()---- {'name': 'saju', 'class_var_num': 100, 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name---- 100 SA-1 [] {}
----e1.assigne_value_to_class_var_str_using_self(200)---- None
----e1.print_all_instance_vars()---- {'class_var_str': 200, 'name': 'saju',
'class_var_num': 100, 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name---- 100 SA-1 [] {}
----e1.assigne_value_to_class_var_list_using_self(300)---- None
----e1.print_all_instance_vars()---- {'class_var_str': 200, 'name': 'saju',
'class_var_num': 100, 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name---- 100 SA-1 [300] {}
----e1.assigne_value_to_class_var_dict_using_self(400)---- None
----e1.print_all_instance_vars()---- {'class_var_str': 200, 'name': 'saju',
'class_var_num': 100, 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name---- 100 SA-1 [300] {'mark': 400}


--------------------------------------------------------------------
----e2.print_all_instance_vars()---- {'name': 'saju'}
----e2.create_new_instance_variable_new_var_1(25)---- None
----e2.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 SA-1 [300] {'mark': 400}
----e2.assigne_value_to_class_var_num_using_class_name(100)---- None
----e2.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 SA-1 [300] {'mark': 400}
----e2.assigne_value_to_class_var_str_using_class_name(200)---- None
----e2.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 200 [300] {'mark': 400}
----e2.assigne_value_to_class_var_list_using_class_name(300)---- None
----e2.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 200 [300, 300] {'mark': 400}
----e2.assigne_value_to_class_var_dict_using_class_name(400)---- None
----e2.print_all_instance_vars()---- >
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 200 [300, 300] {'place': 'Banagalore', 'mark': 400}


--------------------------------------------------------------------
----e3.print_all_instance_vars()---- {'name': 'saju'}
----e3.create_new_instance_variable_new_var_1(25)---- None
----e3.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 200 [300, 300] {'place': 'Banagalore', 'mark': 400}
----e3.reset_class_var_num_using_self()---- None
----e3.reset_class_var_str_using_self()---- None
----e3.reset_class_var_list_using_self()---- None
----e3.reset_class_var_dict_using_self()---- None
----e3.print_all_instance_vars()---- {'class_var_str': 'SA-2', 'name': 'saju',
'class_var_num': 800, 'class_var_dict': {}, 'class_var_list': [], 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict
using class name---- 100 200 [300, 300] {'place': 'Banagalore', 'mark': 400}
----e3.reset_class_var_num_using_class_name()---- None
----e3.reset_class_var_str_using_class_name()---- None
----e3.reset_class_var_list_using_class_name()---- None
----e3.reset_class_var_dict_using_class_name()---- None
----e3.print_all_instance_vars()---- {'class_var_str': 'SA-2', 'name': 'saju',
'class_var_num': 800, 'class_var_dict': {}, 'class_var_list': [], 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 800 SA-2 [] {}