Deprecated functions in Python: How can I make sure... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14

Deprecated functions in Python: How can I make sure...

...that my codes will work with future Python versions? Let's pretend I'm a professional Python developer (I am not) and have written and distributed dozens, maybe hundreds of codes to several clients. Some of them use functions like time.clock() that worked fine up to version 3.3 and now raise an ugly DeprecationWarning, telling me that "time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead". Doesn't that mean that I have to monitor all of the codes I ever wrote and check if they're still compatible with the recent python versions? Should I ask my fictional clients to stay with out of date python versions in order to not risk a DeprecationWarning? Should I pack my whole code in a try/except block and catch/suppress all DeprecationWarnings? Is there any way to handle this professionally? /Edit: try/except doesn't seem to work for DeprecationWarnings (which probably makes sense)

31st Jan 2019, 11:15 AM
Anna
Anna - avatar
5 Answers
+ 16
They probably check new pep's frequently. You can read why they deprecated it here: https://bugs.python.org/issue14309 https://www.python.org/dev/peps/pep-0418/#deprecated-function Also you can suppress DeprecationWarning using this: import warnings import time warnings.simplefilter('ignore', DeprecationWarning) time.clock()
31st Jan 2019, 5:27 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 12
Functions are usually deprecated because there is a clear replacement for what has been deemed a bad practice. The whole idea of deprecating a function instead of immediately rendering it obsolete, is to reduce the impact of breaking backwards compatibility, which is still inevitable. The deprecated annotation is how language devs give that reasonable period of time for programmers to update their existing code and inform their clients (to take specific measures, or just to avoid migrating to the latest version, if relevant). As such, documentation is important to promote traceability. With every single project documented properly, it should not be too hard to go through all the projects and figure out which ones involve deprecated functions, and to plan your countermeasures for each project. (Never worked with PHP, but tis' a popular topic in the community.) : https://softwareengineering.stackexchange.com/questions/191858/keep-a-programming-language-backwards-compatible-vs-fixing-its-flaws
31st Jan 2019, 5:03 PM
Hatsy Rei
Hatsy Rei - avatar
+ 9
Answering to the title (and the query which closely follows), there no way to guarantee, 100%, that all your codes will work in future. Python developers go through the same dilemma and try to not break backwards compatibility as much as we try to make our codes work for the longest period of time possible, into the future. To stay secure in near future, say, an estimate of a decade or so (although going from Python 3.3 to 3.8 took less than a decade IIRC), all we can do is to try not to touch anything which has been deprecated.
31st Jan 2019, 5:17 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
These are kind of issues faced by programmers out there. Nevertheless most of the time it's just some efficiency improvement. Just hope that the deprecation doesn't hit you hard otherwise I don't think you can beat around that easily
2nd Feb 2019, 2:56 AM
Dan Rhamba
Dan Rhamba - avatar
+ 2
This is a problem not only with Python, but with all the programming community in general. Technologies advances with giant steps, and developers should follow those steps. The best you can do is to avoid depreciated functions, and if you see any documentation/tutorial/whatever, check if that is updated
2nd Feb 2019, 3:03 AM
Mati Paredes
Mati Paredes - avatar