Last active
January 4, 2019 15:10
-
-
Save o-y/ae2dfc3a431af94d74daa321cbea3041 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
import ctypes as c | |
class PyObject_HEAD(c.Structure): | |
_fields_ = [ | |
('HEAD', c.c_ubyte * (object.__basicsize__ - c.sizeof(c.c_void_p))), | |
('ob_type', c.c_void_p) | |
] | |
_get_dict = c.pythonapi._PyObject_GetDictPtr | |
_get_dict.restype = c.POINTER(c.py_object) | |
_get_dict.argtypes = [c.py_object] | |
customTypes = { | |
'appendDougnuts': lambda self, number: self + ('o' * number), | |
'oddOrEven': lambda self: '"' + self + '" is: ' + ('Even' if len(self) % 2 == 0 else 'Odd') | |
}; _get_dict(str).contents.value = {**_get_dict(str).contents.value, **customTypes} | |
print ('Hello'.appendDougnuts(33)) | |
#--> Helloooooooooooooooooooooooooooooooooo | |
print ('Helloo'.oddOrEven()) | |
#--> "Helloo" is: Even |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment