You can do this without having to change the language by writing a decorator that enforces the types. The type information is all recoverable from __attributes__.
Be aware though that while enforcing types like int or str is easy, enforcing something list List[int] is less obvious -- you probably won't want to slow down computations with O(N) type checks. IIUC there are existing packages that expose decorators that do this, but I have never tried to use them. --Guido On Tue, Dec 24, 2019 at 6:37 AM Andrew T <andym2...@gmail.com> wrote: > This is a feature PHP has had since PHP7 and is what I use all the time > with PHP. It works on a file by file basis (I guess for backward > compatibility), a declare line needs to be at the top of a file > `declare(strict_types=1);` If this line is at the top of the file, then a > TypeError is thrown on any method/function that uses type hints in the > current file if the passed arg or return value is the wrong type. Here is > more info: > https://dev.to/robdwaller/how-php-type-declarations-actually-work-1mm5 > > I would like to see this PHP feature in Python. A strict types declaration > line can be added at the top any file or/and it could be set globally at > the start of the script by doing something like `sys.strict_types = True`. > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/DS75G2KVPSH62JZMMFDUKPBTLM5CIJ5K/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/33RHFWIESOWDCJTNOR7Z6ZJOIRI47HX7/ Code of Conduct: http://python.org/psf/codeofconduct/