- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
What is the difference between old style and new style classes in Python?
In Python 2.x there's two styles of classes depending on the presence or absence of a built-in type as a base-class −
"classic" style or old style classes have no built-in type as a base class: >>> class OldSpam: # no base class ... pass >>> OldSpam.__bases__ ()
"New" style classes: they have a built-in type as a base class meaning that, directly or indirectly, they have object as a base class −
>>> class NewSpam(object): # directly inherit from object ... pass >>> NewSpam.__bases__ (<type 'object'>,) >>> class IntSpam(int): # indirectly inherit from object... ... pass >>> IntSpam.__bases__ (<type 'int'>,) >>> IntSpam.__bases__.__bases__ # ... because int inherits from object (<type 'object'>,)
When writing a class one would always want to go for new-style classes. The advantages of doing so are numerous, to list some of them −
Support for descriptors. Specifically, the following constructs are made possible with descriptors −
classmethod − A method that receives the class as an implicit argument instead of the instance.
staticmethod − A method that does not receive the implicit argument self as a first argument.
properties with property: Create functions for managing the getting, setting and deleting of an attribute.
Python 3.x supports only new style classes implicitly. There is no need to mention the word object in parentheses in class declaration.
- Related Articles
- What is the difference between static_cast and C style casting?
- What are the differences between the Chicago style and MLA style of writing?
- What is the difference between objects and classes in C#?
- What is the difference between static classes and non-static inner classes in Java?
- What is the difference between interfaces and abstract classes in Java?
- What is the difference between String, StringBuffer and StringBuilder classes in Java explain briefly?
- Python Coding Style Guide
- What is the difference between new/delete and malloc/ free in C/ C++?
- What is the difference between = and == operators in Python?
- What is the Difference Between Scala and Python?
- Difference between Traits and Abstract Classes in Scala.
- What is the difference between the != and operators in Python?
- New and Old Terminologies used in History