AFAIR its impossible. You would like to monkey-patch built-in type, which is impossible in python.
Extended version
Integer literals (1, not int("1")) are always of type int from builtins
/__builtins__
module (depending on python version). What you would like to do is basically copy all methods on unint16
to int class (as method are just attributes of function type). In this case in every place you've stated 1, you'd get an object of type int
, which behaves just like unint16
.
This is impossible in python. I suppose that underlying implementation is getting easier because of this, but there is deeper logic behind this.
int
is one of core classes, like list
, str
, object
, set
, dict
, tuple
, etc. Suppose that someone created VERY handy library that almost everyone would use. If you've ever used Java, then imagine impact on a scale of Apache Commons or Spring. Virtually everyone that counts would use that, because that stuff makes development faster. Now, suppose that that someone created fork - used internally in his company, saying that he done that to keep codebase stable - that has a little piece of code, that changes builtin object
behaviour, so that it sends some crucial data to him. He could scam millions of users on their private data (addresses, which he will then sell, passwords, whatever). Creepy scenario, right?
Now, this doesn't seem very real, but what if you let end-user evaluate python scripts? You probably have seen text editors like gedit) or music players (Rhythmbox) with builtin python shell, providing interface to program internals. Now, soemone, somehow inject change of object
behaviour to your machine. Another creepy scenario.
Those are examples that came to my mind, I can't really remember what did python's creators say about it - that's why I provided link to further discussion.
A little off-topic - this actually MAY lead to some fine libraries ("this" meaning "allowing monkey-patching on builtins"). Imagine monkey patching all bultins so that they are stored in some multiprocessing manager. You virtually get java-style multithreading based on multiprocessing and can somehow avoid GIL. Of course, there are many other reasons why this would not be REALLY good ("illusion of multithreading is not multithreading", for example), but I'd find it useful for simple everyday desktop scripting.