Custom Search

Wednesday, January 26, 2011

main uses of python __class__

main uses of python __class__

>>> l = [2,4]

>>> l
[2, 4]

>>> l.__class__
type 'list'>

>>> l2 = l.__class__()

>>> l2
[]

>>> l.__class__.__name__
'list'

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

>>> l = [2,4]

>>> l
[2, 4]

* Return class (class object) of object l.

>>> l.__class__
type 'list'>

* Creating object of that class (class object) by putting "()" at the end.

>>> l2 = l.__class__()

>>> l2
[]

* To get class name of a python object

>>> l.__class__.__name__
'list'

===================== following are equal

>>> l.__class__
type 'list'>

>>> type(l)
type 'list'>

===================== following are equal

>>> l2 = l.__class__()
>>> l2
[]

>>> l2 = type(l)()
>>> l2
[]

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

* You can get the class (class object) of any object by calling "type" on it.

>>> type(l)
type 'list'>

* we can create object of that class (class object) by putting "()" at the end.

>>> lt = type(l)

>>> lt
type 'list'>

>>> lt() <---------"()" at the end.
[]

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

No comments:

Post a Comment