Skip to content

Instantly share code, notes, and snippets.

@ansig
Created January 30, 2016 07:01
Show Gist options
  • Save ansig/14b2cb083950b2403c0f to your computer and use it in GitHub Desktop.
Save ansig/14b2cb083950b2403c0f to your computer and use it in GitHub Desktop.
Demonstrate some differences between old-style classes and new-style classes in Python
class OldStyle():
def __init__(self):
print "Init old style"
def func(self, arg):
print "Invoked old func with: {}".format(arg)
class NewStyle(object):
def __init__(self):
print "Init new style"
def func(self, arg):
print "Invoked new func with: {}".format(arg)
class OldSubClass(OldStyle):
def __init__(self):
print "Init old sub class"
OldStyle.__init__(self)
def func(self, arg):
print "Invoked old subclass func with: {}".format(arg)
OldStyle.func(self, arg)
class NewSubClass(NewStyle):
def __init__(self):
print "Init new sub class"
super(NewSubClass, self).__init__()
def func(self, arg):
print "Invoked old func with: {}".format(arg)
super(NewSubClass, self).func(arg)
old = OldSubClass()
print "Old type: {}".format(type(old))
print "Old class: {}".format(old.__class__)
old.func('foo')
new = NewSubClass()
print "New type: {}".format(type(new))
print "New class: {}".format(new.__class__)
new.func('bar')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment