Created
January 30, 2016 07:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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