-
-
Save mambocab/273a5916c2481e82e07acb77535fa263 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
class ClassWithDefaults(object): | |
def __init__(self, arg1, arg2, default1=False, default2=False): | |
(self.arg1, self.arg2, | |
self.default1, self.default2) = (arg1, arg2, | |
default1, default2) | |
def __repr__(self): | |
return('{}(arg1={}, arg2={}, default1={}, default2={})'.format( | |
self.__class__.__name__, | |
self.arg1, self.arg2, | |
self.default1, self.default2 | |
)) | |
def set_default_flag(): | |
defaults = list(ClassWithDefaults.__init__.__defaults__) | |
print(defaults) | |
defaults = (defaults[:1] + [True] + defaults[2:]) | |
print(defaults) | |
try: | |
ClassWithDefaults.__init__.__defaults__ = tuple(defaults) | |
except: | |
ClassWithDefaults.__init__.__func__.__defaults__ = tuple(defaults) | |
if __name__ == '__main__': | |
x = ClassWithDefaults('arg1', 'arg2') | |
print(x) | |
set_default_flag() | |
x = ClassWithDefaults('arg1', 'arg2') | |
print(x) |
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
class ClassWithDefaults(object): | |
def __init__(self, arg1, arg2, default1=False, default2=False): | |
(self.arg1, self.arg2, | |
self.default1, self.default2) = (arg1, arg2, | |
default1, default2) | |
def __repr__(self): | |
return('{}(arg1={}, arg2={}, default1={}, default2={})'.format( | |
self.__class__.__name__, | |
self.arg1, self.arg2, | |
self.default1, self.default2 | |
)) | |
def set_default_flag(): | |
defaults = list(ClassWithDefaults.__init__.__defaults__) | |
print(defaults) | |
defaults = (defaults[:1] + [True] + defaults[2:]) | |
print(defaults) | |
try: | |
ClassWithDefaults.__init__.__defaults__ = tuple(defaults) | |
except: | |
ClassWithDefaults.__init__.__func__.__defaults__ = tuple(defaults) | |
if __name__ == '__main__': | |
x = ClassWithDefaults('arg1', 'arg2') | |
print(x) | |
set_default_flag() | |
x = ClassWithDefaults('arg1', 'arg2') | |
print(x) | |
Interpreted: | |
==================== | |
ClassWithDefaults(arg1=arg1, arg2=arg2, default1=False, default2=False) | |
[False, False] | |
[False, True] | |
ClassWithDefaults(arg1=arg1, arg2=arg2, default1=False, default2=True) | |
==================== | |
Cythonized: | |
==================== | |
ClassWithDefaults(arg1=arg1, arg2=arg2, default1=False, default2=False) | |
[False, False] | |
[False, True] | |
ClassWithDefaults(arg1=arg1, arg2=arg2, default1=False, default2=False) | |
==================== |
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
cat ./defaults_demo.py | |
echo Interpreted: | |
echo '====================' | |
python defaults_demo.py | |
echo '====================' | |
cython --embed -o defaults_demo.c defaults_demo.py | |
gcc -Os -I /usr/include/python2.7 -o defaults_demo defaults_demo.c -lpython2.7 -lpthread -lm -lutil -ldl | |
echo Cythonized: | |
echo '====================' | |
./defaults_demo | |
echo '====================' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment