-
-
Save toshok/1fd35ed8b7166b5dfc67 to your computer and use it in GitHub Desktop.
This file contains 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
# 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