-
-
Save pmav99/137dbf4681be9a58de74 to your computer and use it in GitHub Desktop.
Code below is tested with 2.5, 2.7 and 3.3
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
from functools import wraps | |
import inspect | |
# Fixed code | |
def initializer(fun): | |
names, varargs, keywords, defaults = inspect.getargspec(fun) | |
@wraps(fun) | |
def wrapper(self, *args, **kargs): | |
for name, arg in zip(names[1:], args) + kargs.items(): | |
setattr(self, name, arg) | |
for i in range(len(defaults)): | |
index = -(i + 1) | |
if not hasattr(self, names[index]): | |
setattr(self, names[index], defaults[index]) | |
fun(self, *args, **kargs) | |
return wrapper | |
class Foo(object): | |
@initializer | |
def __init__(self, a, b, c=None, d=None, e=3): | |
pass | |
f = Foo(1, 2, d="a") | |
assert f.a == 1 | |
assert f.b == 2 | |
assert f.c is None | |
assert f.d == "a" | |
assert f.e == 3 | |
print("Ok") |
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
from functools import wraps | |
import inspect | |
# Python3 compatible code (it runs fine under python2 too). | |
def initializer(fun): | |
names, varargs, keywords, defaults = inspect.getargspec(fun) | |
@wraps(fun) | |
def wrapper(self, *args, **kargs): | |
for name, arg in list(zip(names[1:], args)) + list(kargs.items()): | |
setattr(self, name, arg) | |
for i in range(len(defaults)): | |
index = -(i + 1) | |
if not hasattr(self, names[index]): | |
setattr(self, names[index], defaults[index]) | |
fun(self, *args, **kargs) | |
return wrapper | |
class Foo(object): | |
@initializer | |
def __init__(self, a, b, c=None, d=None, e=3): | |
pass | |
f = Foo(1, 2, d="a") | |
assert f.a == 1 | |
assert f.b == 2 | |
assert f.c is None | |
assert f.d == "a" | |
assert f.e == 3 | |
print("Ok") |
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
from functools import wraps | |
import inspect | |
# Original code | |
def initializer(fun): | |
names, varargs, keywords, defaults = inspect.getargspec(fun) | |
@wraps(fun) | |
def wrapper(self, *args, **kargs): | |
for name, arg in zip(names[1:], args) + kargs.items(): | |
setattr(self, name, arg) | |
for i in range(len(defaults)): | |
if not hasattr(self, names[-(i+1)]): | |
setattr(self, names[-(i+1)], defaults[i]) # defaults[i] should be defaults[-(i + 1)] | |
fun(self, *args, **kargs) | |
return wrapper | |
class Foo(object): | |
@initializer | |
def __init__(self, a, b, c=None, d=None, e=3): | |
pass | |
f = Foo(1, 2, d="a") | |
assert f.a == 1 | |
assert f.b == 2 | |
assert f.c is None # This fails (f.c == 3) | |
assert f.d == "a" | |
assert f.e == 3 # This fails (f.e is None) | |
print("Ok") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment