I was wondering if it’s possible to wrap some class methods with a decorator that parses the arguments before sending it to the function. for example:
class integer(int):
def __init__(self, value=0)
self.value = value
for m in ['__add__','__sub__','__mul__']:#and so on
method = getattr(self, m)
method = magic_decorator(method)
...
given that magic_decorator would be a class or function that captures the single argument from these methods and parse than, for example if it would be a string, instead of letting it in to throw an exception, try to parse as a integer with int first.
That would be great for creating even more pythonic types from the native ones.
If it’s not possible to do in this stylish way, I would have to override each one doing a repetitive job on each one, which wouldn’t be pythonic.
EDIT:
“string”-object or integer-object wouldn’t work even so, i would love to know how do I work around this. =]
I didn’t a exhaustive search for duplicates, but I’m quite sure that isn’t one ’cause my question is a little bit too specific.
Sorry for my bad English, I hope you understand, thanks in advance.
Yes it is possible. Just overwrite every method.
But be careful, decorators are slowing things dowwwwn.
You can just overwrite every method:
Or you can do it the easy way: