Old Classes Versus New Classes Still Matters In 3.0
@ 2009-01-05 21:38:37
Filed under: Code Tech Python
I was reading a book outlining Python 3.0 changes and I noticed all the classes he made looked like Old Style classes to me. So I had to take a look for myself to see if there was still a difference. Though it looks like much less than it use to ... but there is at least a difference in name still ...
Here is the output from runs on 2.5 and 3.0.
If your writing new code for 2.x or 3.x please use New Style classes!
digg it
seed it
del.icio.us
ma.gnolia
Log in to post comments.
Filed under: Code Tech Python
I was reading a book outlining Python 3.0 changes and I noticed all the classes he made looked like Old Style classes to me. So I had to take a look for myself to see if there was still a difference. Though it looks like much less than it use to ... but there is at least a difference in name still ...
import platform
class OldStyle:
"""
An old style class. Boo!
"""
def __init__(self):
"""
Create a Old Style class and prints info about itself.
"""
print("%s\n\tType: %s" % (self.__class__.__name__, type(self)))
print("\tStructure: %s" % dir(self))
class NewStyle(object):
"""
An new style class. Use this!
"""
def __init__(self):
"""
Create a New Style class and prints info about itself.
"""
print("%s\n\tType: %s" % (self.__class__.__name__, type(self)))
print("\tStructure: %s" % dir(self))
if __name__ == '__main__':
print("Python Version: %s" % platform.python_version())
OldStyle()
NewStyle()
Here is the output from runs on 2.5 and 3.0.
[steve@powerhouse ~]$ python objects.py
Python Version: 2.5.2
OldStyle
Type: <type 'instance'>
Structure: ['__doc__', '__init__', '__module__']
NewStyle
Type: <class '__main__.NewStyle'>
Structure: ['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
[steve@powerhouse ~]$ /opt/python3/bin/python3.0 objects.py
Python Version: 3.0.0
OldStyle
Type: <class '__main__.OldStyle'>
Structure: ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__']
NewStyle
Type: <class '__main__.NewStyle'>
Structure: ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__']
If your writing new code for 2.x or 3.x please use New Style classes!
digg it
seed it
del.icio.us
ma.gnolia
Log in to post comments.

