Difference between new_style and old_style class?

Last updated 3 weeks, 4 days ago | 23 views 75     5

Tags:- Python

Python | new_style and old_style

Old-style: With old-style classes, class and type are not quite the same things.  If obj is an instance of an old-style class, obj __class__ designates the class, but the type(obj) is always an instance.

>>> class Foo:
...     pass
>>> x = Foo()
>>> x.__class__
<class __main__.Foo at 0x000000000535CC48>
>>> type(x)
<type 'instance'>

New-Style: New-style classes unify the concepts of class and type. If obj is an instance of a new-style class, type(obj) is the same as obj. __class__:

>>> class Foo:
...     pass

>>> obj = Foo()
>>> obj.__class__
<class '__main__.Foo'>
>>> type(obj)
<class '__main__.Foo'>
>>> obj.__class__ is type(obj)


For more details click here