Sunday, December 25, 2011

Detecting new- and old-style classes

Be it typos, old libraries, or just backwards-compatibility, old-style classes are Python relics that have a tendency to creep into your shiny new programs. Given that new-style classes are almost certainly what you want, it can't hurt to know how to detect the difference.

>>> # Using Python 2
>>> class old_style(): # doesn't inherit from object
... pass
>>> class new_style(object): # does inherit from object
... pass
>>> old_object = old_style()
>>> new_object = new_style()
>>> type(old_object) == old_object.__class__
>>> type(new_object) == new_object.__class__

For the inquisitive, new-style classes unified the concept of class and type in Python 2.2. For a complete description, check out Guido's essay on the topic, circa 2002.

Merry Guido-mas, and may you only find new-style classes under your AST.


  1. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing.
    Python Training in Chennai

  2. First time I am visiting this blog. I found so many entertaining stuff in this blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the excellent work. Check for essay writing service reviews

  3. Marvelous blog with tons of valuable information. I gathered some useful information through your blog. Want to learn python then visit

    python Online Training

  4. Your article is very good and useful, thank you very much for this content. : usefull site