Skip to content

Instantly share code, notes, and snippets.

@toshok
Created April 14, 2015 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toshok/1fd35ed8b7166b5dfc67 to your computer and use it in GitHub Desktop.
Save toshok/1fd35ed8b7166b5dfc67 to your computer and use it in GitHub Desktop.
# this one works because even though we define __new__ and not __init__,
# tuple.__init__ takes the same number of args as what we pass to Tuple1's
# ctor
class Tuple1(tuple):
def __new__(self, iter):
return super(Tuple1, self).__new__(self, iter)
f = Tuple1([1,2,3])
print f
# this one works for the same reason as Tuple1, but with __new__ and __init__
# swapped.
class Tuple2(tuple):
def __init__(self, iter):
return super(Tuple2, self).__init__(self, iter)
f = Tuple2([1,2,3])
print f
# this one fails because even though we define an __init__ method
# that takes the right number of args, since we don't override __new__,
# tuple.__new__ doesn't match the arity.
class Tuple3(tuple):
def __init__(self, iter, another):
return super(Tuple3, self).__init__(self, iter)
try:
f = Tuple3([1,2,3], None)
print f
except Exception as e:
print e
# this one does *not* fail, even though we pass the incorrect number
# of args to the ctor and fail to override __init__.
class Tuple4(tuple):
def __new__(self, iter, another):
return super(Tuple4, self).__new__(self, iter)
try:
f = Tuple4([1,2,3], None)
print f
except Exception as e:
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment