Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
Python Programming

New Python 3.9 'Brings Significant Changes' To Language Features (infoworld.com) 74

This week's release of Python 3.9 "brings forward significant changes to both the features of the language and to how the language is developed," writes InfoWorld — starting with a new yearly release schedule and performance-boosting parser improvements: - Python makes it easy to manipulate common data types, and Python 3.9 extends this ease with new features for strings and dictionaries. For strings, there are new methods to remove prefixes and suffixes, operations that have long required a lot of manual work to pull off. [The methods are named .removeprefix() and .removesuffix() and their return value is the modified string]

- For dictionaries, there are now union operators, one to merge two dictionaries into a new dictionary and one to update the contents of one dictionary with another dictionary.

- Decorators let you wrap Python functions to alter their behaviors programmatically. Previously, decorators could only consist of the @ symbol, a name (e.g. func) or a dotted name (func.method) and optionally a single call (func.method(arg1, arg2)). With Python 3.9, decorators can now consist of any valid expression...provided it yields something that can function as a decorator...

- Two new features for type hinting and type annotations made their way into Python 3.9. In one, type hints for the contents of collections — e.g., lists and dictionaries — are now available in Python natively. This means you can for instance describe a list as list[int] — a list of integers — without needing the typing library to do it. The second addition to Python's typing mechanisms is flexible function and variable annotations. This allows the use of the Annotated type to describe a type using metadata that can be examined ahead of time (with linting tools) or at runtime...

- Python extension modules, written in C, may now use a new loading mechanism that makes them behave more like regular Python modules when imported.

This discussion has been archived. No new comments can be posted.

New Python 3.9 'Brings Significant Changes' To Language Features

Comments Filter:
  • by Z00L00K ( 682162 ) on Saturday October 10, 2020 @03:39PM (#60592516) Homepage Journal

    Like ensuring that the complete code is consistent before starting to execute?

    In the current Python you won't get any warning until you try to execute a function that don't exist if it's in a different file, then the program bombs. So this means that you need 100% test coverage of the code for every release.

    Compared to languages like Java and C# where that kind of issue is discovered at compile time.

    • by Antique Geekmeister ( 740220 ) on Saturday October 10, 2020 @03:50PM (#60592536)

      Testing code for "consistency" is an NP complete problem. It's complicated by tendency of Python developers to use "pip install" to pull in arbitrary modules at run time, which is not deterministic, and tracing down the dependencies is itself an NP complete problem due to the dependency chains and deliberate re-wrapping of some modules by other modules.

      • by Z00L00K ( 682162 )

        The way you describe it makes me think of storing sodium in biodegradable plastic bags in a barrel of water.
        One day you will get an interesting surprise.

        • I've already experienced numerous such surprises, thank you. Many languages that "dynamically install modules as needed" have created just such surprises when the production version winds up with different versions of the same modules than were installed at the time of the test installation.

          • Many languages that "dynamically install modules as needed [...]

            Only thing, that's not a Python language feature.

            Some people have a build setup where the CI 'pip installs' everything from scratch every time, or a test setup where they "pip install -r requirements.txt" into a virtualenv every time they run the test. But that's just their build setup. They could stop doing that in an instant, and their program would still run fine.

            If someone deploys to production in a way that's automated along those lines, with a requirements file that pulls in the latest and greatest

            • Take a look in /usr/bin/ on any system with python. Can you find any python scripts that do not import python modules installed separately? Systems that attempt to package their python modules provide some help stabilizing them, how many of us suffer the same difficulties we encounter with ant, maven, and perl where the dependency chains are not necessarily stable? It's fundamental not only to the language, but to the packaging of modules at pypi.org. The problem is exacerbated by modules like "bs4", which

              • I'm kinda confused as to how your problems with pip are fundamental to the Python language, when apparently you have the same kind of problems with ant and maven, which are written in a language very much different from Python.

                Take a look in /usr/bin/ on any system with python. Can you find any python scripts that do not import python modules installed separately?

                Well, yes I can. I went looking, and the first Python script I found was smtpd.py, which imports sys, os, errno, getopt, time, socket, asyncore and asynchat. Standard library only.

                Can you find any C programs that don't use dynamically-linked shared objects installed separately?

                • I neglected ruby and rubygems, which have the same issue.

                  > I went looking, and the first Python script I found was smtpd.py

                  smtpd.py is part of the basic Python 3 source code, Python-[version]/Lib/smtpd.py. I'm nto surprised that it has no other modular requirements. I _did_ say to look in /usr/bin/. I suspect that is not where you found it. If you did find it there, what Python installation and operating system are you working with?

                  The same problem _can_ exist for C programs. "Mint" Linux is an infamous

                  • I _did_ say to look in /usr/bin/. I suspect that is not where you found it.

                    That's where I found it.

                    • How did that wind up in /usr/bin/smtpd.py? That script is normally installed in the _library_ directory for python, /usr//lib/python[version]/smtpd.py. What operating system's python setup are you using that it got placed or linked to /usr/bin/smtpd.py ? Might someone have placed or linked it there manually?

    • Isn't this just a dynamic vs static typing issue? I could be wrong, but I don't think dynamically typed languages can be compiled ahead of time. I don't disagree with you. in fact, I hate dynamic typing. But my point is that this issue is not unique to Python, and I don't think it has a resolution as long as Python is dynamically typed. And removing dynamic typing from Python would cause it to stop being Python.
      • by Z00L00K ( 682162 )

        No, that problem doesn't have anything to do with typing.

        Consider the following, 2 python files:
        f2.py

        def print_world():
        print ("World!")

        f1.py

        import f2
        import random

        rand=random.random();
        flag = 0
        if rand > 0.5:
        flag = 1

        print (rand)
        print ("Hello ")

        f2.print_world()

        if flag == 1:
        f2.print_life()

        This example will randomly give the following error:

        Traceback (most recent call last):
        File "f1.py", line 15, in

        • by MtHuurne ( 602934 ) on Saturday October 10, 2020 @05:40PM (#60592738) Homepage

          The Python VM doesn't check that, but there are various static analysis tools that will, such as pyflakes, pylint and mypy. The problem from this example would be detected by pyflakes, which requires no annotations and runs super quick. More complex problems might require type annotations and mypy to detect. These days you can use Python as a dynamically typed language or a statically typed one or anywhere inbetween.

          • But why isnt the compiler/interpreter doing it?
            • Raise a PEP suggesting it.

            • by jythie ( 914043 )
              It is doing it, that is why you get the error message. But because everything is mutable in python, the interpreter has no way of guessing what might be done to f2 over time. You can add methods, you can remove methods, you could load a whole other object into the same space, et. There is no point before you try to access the method that it makes any sense for the interpreter to throw an error.
        • by orlanz ( 882574 )

          This isn't something a scripting/interpeted language should consider. Most complied languages don't either (ie: reflections). You are asking the runtime to trace through all paths and also all permutations. In Python, being dynamic, that namespace could be changed/side-effected at anytime, and the quack could be valid now.

          But the point is the execution should be as fast as possible, it shouldn't waste time thinking of all the potential possiblities and error conditions for each and every run. Compiled l

          • by Z00L00K ( 682162 )

            This isn't something a scripting/interpeted language should consider. Most complied languages don't either (ie: reflections). You are asking the runtime to trace through all paths and also all permutations.

            No, I'm asking the runtime to just check that all calls have an implementation before starting to execute the whole thing.

            Compiling languages do that either during compilation like Java or C# or during linking like C/C++/Fortran etc.

            This is why using languages like Python where no checks are done to verify that everything is connected should be seen as risky languages for large scale production.

            The dynamically selected imports is something that's convenient for flexibility but really scary from a long term

            • What I am saying is that in reflection and scripting based languages, not all calls are defined at runtime. Some of them are defined as part of the execution. So depending on the execution path, not all calls have to be valid... only the one on the execution path. And to validate you would need to figure out all execution paths and validate the pre-requisites of each.

              And on top, for scripting languages, it's a pretty big penalty considering it's done on _every_ run.

            • by jythie ( 914043 )
              Ahm.. you do realize that compiled languages also support dynamic loading, right? Or for that matter, all of those languages support (or even require) the use of external libraries. They can check a call vs a header, but they can still fail to find a symbol when you link to a library, and that library can be swapped out at any time by the user/system/whatever.
        • by eddeye ( 85134 )

          Because it hasn't verified at the beginning that the function print_life() actually exists so it will bomb only when the execution reaches that point.

          Guy who doesn't understand dynamic binding [wikipedia.org] complains about dynamic binding. Film at 11.

          Have you ever taken a programming course in your life? Or did you just rummage through a dog-eared copy of Java for Dummies you found lying in the gutter?

          Dynamic binding is there for a reason. Hint: it's not for you. Go back to your nice safe Java straitjacket an

          • by Z00L00K ( 682162 )

            Dynamic binding is there for a reason. Hint: it's not for you. Go back to your nice safe Java straitjacket and use the safety scissors so you don't stab your own eye out. Let the grown ups handle the nasty wasty Python.

            Whenever I see that statement I see that Python is perfect for academic research into potential software solution but it's a solution that is dangerous in production environments where long term stability is essential and it's a big headache to figure out why something seemingly unrelated stopped working for no apparent reason. And as I have seen in another comment - if the dynamically loaded items are on a remote service then you are in a world of pain if that service goes offline or changes the behavior.

            A

            • by jythie ( 914043 )
              Ahm.. ok, so I have a similar career length, and I think you REALLY have the wrong end of the stick there. What you are describing has nothing to do with Python, that type of problem exists in pretty much any modern language and any shop that has to use it in a production environment knows how to manage upstream library changes. Very few systems still use the 'static linking only' deployment pattern.

              But yeah, all the problems you are describing I've run into in C, C++, Java, and even FORTRAN. The headac
          • by jythie ( 914043 )
            Heh. Last major Java project I worked on made heavy use of 3rd party instnationtation.. I wish I could remember the name of the system (blanking on it right now), but a lot was done via xml files that instructed the application which jar files to load and what to execute in them. So Java.. not that safe.
          • by orlanz ( 882574 )

            But just to be clear for others, all languages that seem to be statically binded have their dynamic equivalents within. Java appears static in its interfaces and OOP model, but has reflections that bypass all that static stuff. C/C++ have the preprocessor that does dynamic code enablement and void* for "unknown"; it is a exercise to the programmer to validate all calls and avoid execution paths with invalid calls. Assembly is ALL just dynamic with a "If it fits, ok, else that's OK too!" mindset that make

    • Re: (Score:3, Informative)

      by LionKimbro ( 200000 )

      I honestly do not understand what practical problem it is that you have with this.

      It sounds like: "If I do not know that something has mechanically checked that every referenced resource is in fact present and available, then the language is insufficient for my need for security."

      But honestly, man, how do you know that somebody hasn't replaced parts of the content of your executable with gibberish? How do you know that your program, referencing a critical text file, -- that the text file hasn't been delete

      • by _merlin ( 160982 )

        In a compiled language like C or Java, if you remove or rename a function, or change its arguments, you'll get compile errors for any uses of the function that you haven't updated. In Python or JavaScript, you need to exercise every single code path to make sure nothing is still expecting the old function signature. You can't use a grep for the function name to help, because you can easily take a bound reference to it and pass that around.

        Why is it so hard for people to understand that having these kinds

        • Python has plenty of tools to enforce strictness. We use mypy and other tools to avoid the issues you mention as part of our CI pipelines on every pull request and the use cases you describe are pretty much a non issue for us, to some extent as I mention below.

          Iâ(TM)ve had runtime issues with Java where monolithic projects running on tomcat would end up with conflicting versions of common third party libraries and they would fail at runtime because some developers will get out of their way to shade dep

        • by jythie ( 914043 )
          This is not actually true, or more specifically, it is only true if you are using static linkings. As soon as you start using dynamic linking, you can have programs pass their compile time checks because the source matches the header, but it can still fail to find symbols on library load. For small/medium sized code bases I see what you are getting at, but large complex projects often have more modularity and utilize various types of dynamic linking and thus encounter the same problems.

          But the bigger is
    • Compared to languages like Java and C# where that kind of issue is discovered at compile time.

      Huh? Both those languages load classes dynamically at runtime.

      Maybe you meant C++.

      • by Z00L00K ( 682162 )

        Just try to build your application in Java or C# without having the ends tied together and you'll see what happens.

        Of course you can build one class individually, but that's often not feasible, you usually build the system in an IDE like Eclipse or Visual Studio and there you'll see when it's broken.

        • by jythie ( 914043 )
          I can not speak for C#, but a lot of big Java projects make heavy use of dynamic loading, which means the compiler is not able to detect such things either. How in the world would it know a function is missing in something it has not loaded yet, esp when the dynamic loading is handled via config files rather than hardwired into the code.
    • Maybe because Python is interpreted is why compile time doesnâ(TM)t do what you think it should

    • by jythie ( 914043 )
      Well, unless you are using dynamic library loading, in which case languages like Java will fail in the same way. That is kinda the cost of such flexibility.
    • I don't do much python, but isn't python supposed to be a dynamic language, like Lisp, Javascript or Perl?

      If yes, then what you're asking for is impossible. Use C++ / etc if you want strong compile time checks.

      If no, then again use C++, because you won't lose anything in term of flexibility, and you will get not only compile time checking, but also faster and lighter programs ;-)

  • by Glasswire ( 302197 ) on Saturday October 10, 2020 @03:47PM (#60592534) Homepage

    Case, switch, don't care what you call it but 7 layer nested if statements are insane.
    If they're a bad idea why does virtually EVERY other language have them?
    When you're lacking control constructs the shell scripting language has there's a problem.

    • Not having case statements is a minor annoyance, but a seven part "if ... elif ... elif ... else" statement is not nested. It's a single statement, the main difference with "case" is just that you need to repeat the "foo == " part at each step. I don't really see that as a huge deal breaker.

      • by teslar ( 706653 ) on Saturday October 10, 2020 @06:20PM (#60592828)
        I believe the pythonic way of implementing a switch statement is to use dictionaries (values can be functions or lambdas too); not an if/elif construct.
        • I know, but with the restrictions on anonymous lambdas, constructing such a dictionary with a bunch of named functions is a hassle and doesn't make one cohesive statement.

        • This is the correct python way, closures and Lambdas are often confused with one another and so people defer to the old case or if/elif because thats was you had in in older popular languages like php.

        • by _merlin ( 160982 )

          And that's yet another example of Python doing things in an inefficient way. You need to build a hash table in the heap and then call through a bound function reference, and you can only get results out of the function by return value because they're another level of scope, so you can't just assign to variables. It can get you a similar result to a switch statement, but it requires ugly code and it's inefficient.

      • the main difference with "case" is just that you need to repeat the "foo == " part at each step.

        The fun part will come when arguments over what methodologies switch/case would actually support surface.

        Honestly it seems to me that first class Function Tables with first class Lambdas would be most versatile. In such a case (pun) switch/case semantics are but one way of walking and processing such tables, or pairs of tables (one table the conditions, the other the actions)

        But on a language that uses fucking whitespace... yeah... never gonna have anything nice

    • When you're lacking control constructs the shell scripting language has there's a problem.

      How do you know what shell I'm using?

  • by Myself ( 57572 ) on Saturday October 10, 2020 @04:14PM (#60592572) Journal

    I can't be the only one who's tried to use Python code I found on the internet and had no end of trouble because there are so many mutually incompatible versions of the language, versions of libraries, etc.

    IMHO, "significant changes" should be absolutely the last thing a language ever tries to do. When you reach the point of "significant changes", it's time to call it something different and stop pretending it's the same language, because it isn't.

    • Or at least to call it version 4.0 rather than 3.9, to indicate the breaking changes.

      • Or at least to call it version 4.0 rather than 3.9, to indicate the breaking changes.

        If there were any...The list in TFS doesn't mention any backwards incompatibilities, just added features. Releasing a new minor version for it seems perfectly consistent with semantic versioning https://semver.org/ [semver.org] conventions to me.

        • by _merlin ( 160982 )

          Oh, but there are breaking changes. They've removed ".tostring()" from "struct" and its replacement ".tobytes()" was only introduced in 3.2, so if you want your code to work with both 3.1 and 3.9 you need to do stuff like this: offsdata.tobytes() if hasattr(offsdata, "tobytes") else offsdata.tostring()

          • As far as I know it's advised that you don't use python versions 3.0 - 3.3, I think a lot of people think mistakes were made and 3.4 was the first version that improved on 2.7. Obviously this it's self is an issue as some people might not know but I've seen things to that effect stated in many places so I'd suspect if it might affect you, you'd probably know about it.
    • by stesch ( 12896 )
      That's one of the problems of languages that are tied too much to one significant implementation.
    • Is it that hard to use the python 2 interpreter for python 2 code and the python 3 interpreter for python 3 code?
      • It seems so. While you can have multiple versions of the Python interpreter installed on a system, each with their own sets of installed modules, actually getting the selecting the right one at run time can be tricky at best. On *nix systems you could just spell it out with a shebang line. On Windows you have hacks like PyLauncher that is associated with all .py files and then uses heuristics (or looks for a .ini file) to figure out whether it should launch a given script with the Python 2 or Python 3 inter
        • On *nix systems you could just spell it out with a shebang line.

          And on Windows, ditto. If you write python2 on the shebang line, that's what you get.

          Just add that to your existing Python 2 programs, ensure that Python 3 is the default otherwise (which it probably is without you having to do anything), and you're all set. It's not like you're going to be writing new Python 2 programs, is it?

      • by Mozai ( 3547 )
        I can think of a few reasons:
        • devs keep assuming everyone's computer is identical to their computer, so when they say "just launch the python interpreter," one dev will assume everyone has "python" and "python3" and another dev will assume everyone has "python" and "python2".
        • this goes double for whether "python3" means Python 3.6, Python 3.7, Python 3.8 or Python 3.9
        • Each version of python needs its own verison of the interpreter and standard library and add-on modules, which can sum up to over a hundr
      • by jythie ( 914043 )
        depends on if you want the two code bases to talk to each other or not. One of the problems with these constant changes is they make code reuse more difficult, both in terms of hooking your existing code up to older libraries, or hooking your current code to a library written for a newer version.
    • by jythie ( 914043 )
      Well, at least this time it looks like they are just adding some new methods as opposed to changing the language.. but yeah, Python's constant changes have made it a frustrating language to use for long term projects. Though TBH, I find the 'dialect' problem in Python to a bigger issue. Since Python is so dynamic and it is easy to change how the language operates, there are multiple dialects of Python where people have reworked the language to behave like some other language they would rather be using...
  • Too bad I can't understand the esoteric details of this story. Looking at the early comments it seems their understanding of Python is even lower than mine. For example, there's a major difference between interpreted and compiled languages...

    However in terms of improving my own Python skills and understanding, maybe this is a good place to ask? I'm looking for a moderate-sized project to hack at. That's my favorite way to learn new languages these years. Take working code and rework it, naturally learning f

    • by Z00L00K ( 682162 )

      Too bad I can't understand the esoteric details of this story. Looking at the early comments it seems their understanding of Python is even lower than mine. For example, there's a major difference between interpreted and compiled languages...

      See it the other way - the experience from compiled languages gives a lot of experience on what's wrong with interpreting languages. Each time I look at Python I see a language where there's a potential for a lot of problems in the long run. Python isn't alone, PHP suffers from similar problems as well so you can't update the version of the interpreter without breaking a lot of stuff on your web server.

      Python is basically repeating some of the mistakes made by interpreting Basic in the 80's.

      • by shanen ( 462549 )

        I think you're getting diverted into areas of language theory, but a lot of my experience was with JIT (Just In Time) Java compilers that were trying to split the hairs. I remember multi-level compilation and recompilation as the hot code was identified on the fly... But this was really going to an intermediate language that reminded me of the old p-code.

        However I think Python was designed to avoid a lot of the interpretation problems and learned more lessons along the way. The more learned response earlier

        • by Z00L00K ( 682162 )

          See an example I made above on one of the issues I have with Python. It applies to other scripting languages as well though.

          • Now I think you were the source of the "learned response" that I referenced. Not certain, because I'm mobile and the mobile interface to Slashdot still reeks.

            • by Z00L00K ( 682162 )

              If you mean "learned response" through someone "told me so" you are wrong. My "learned response" is because this is what I have discovered while fiddling with a lot of various languages during 40 years and discovered what works long term and what doesn't. Python (and other scripting languages) is like building on a place where the land is flat because it's easy just do discover a few years later that it's on clay base and suddenly you end up in the software variant of this: https://upload.wikimedia.org/w... [wikimedia.org]

              • by shanen ( 462549 )

                Well, now you've triggered my nostalgia module. It's been about that long since I studied computer science formally. In those days I got to fiddle with a lot of languages and I found it helpful in many ways. Most of the highlights are lost in the mists of time, though I remember getting to do APL on a machine with an actual APL keyboard with all the weird symbols on it. (The only time I got to use APL in the real world it was with a normal keyboard and I had to learn all the dot codes for the weird characte

            • The idea that a language needs to be either difficult to use and compiled or easy to use, no static typing and interpreted comes from C/*nix. Shell/Perl vs C. It is plain wrong.

              That is what Lisp, thence Java and .Net introduced. Languages that were easy to use and yet could be compiled efficiently. No dichotomy.

              Modern JavaScript systems also compile to native in a strange sort of way. Start with a nasty language with no static typing, and then do incredibly clever analysis and compiler tricks to compil

              • by shanen ( 462549 )

                Not another nostalgia trigger? Lisp was one of my favorite languages. I remember when I was supporting the development team for one of the last real Lisp machines. No OS, just Lisp running right out of the microcode. My most interesting bug turned out to be in the compiler... I spent most of a morning trying to figure out it if was broken before bothering the actual expert, who spent about 30 seconds confirming the bug and then told me who was responsible for fixing it. (Really nice guy to give me the credi

    • by Anonymous Coward

      The way i see Python is, if you, a researcher for example, needs to hack together an application to process a bunch of data, Python is just perfectly adequate.

      However, if i was was big-corp and would rely my code base to be built upon Python, in the long run i'll get what i deserve - a disaster. It's not even hypothetical, python 2 is now curated by private entities.

      Or if i work in a big team on a complicated big project - a disaster is around the corner anyways so using Python should really be justified by

      • C++ is not really a beautiful language. It seems to be in the dark zone between C and Java/C#. And it has some parts that are quite cryptic.

        For new development I'd limit the use of C++ to a minimum.

      • by jythie ( 914043 )
        Heh. I've actually been seeing big corps moving from Java to Python. As long as you lock your developers to a specific version it can be a surprisingly stable language as long as you have good version control and change management procedures. My current project transitioned from Java to Python a few years back and we are up to about a million lines. As more 3rd party libraires reach maturity I expect more and more teams will transition off Java,.. which, given how much of Java has moved over to dyn
  • by 93 Escort Wagon ( 326346 ) on Saturday October 10, 2020 @04:37PM (#60592604)

    "Python extension modules, written in C, may now use a new loading mechanism that makes them behave more like regular Python modules when imported."

    Behave more like regular python modules... so, that means they now run slower?

  • Just what python needs .. big changes. How did that work out python 3.0? People are STILL on python 2.7.

    Python 4.0 they should just become Fortran or something like that. No big deal to migrate from 3.9 to 4.0 just convert all your code into Fortran.

    • Part of the problem is half of the developers creating modules in python are not programmers by definition, they are data scientist that learned to code in python and have no intention of using the latest and greatest version of python.

      There are other issues to, redhat and mac OS both used 2.7 by default for the os....and what would often happen is people would upgrade the os version possibly causing issues for the os and whatever packages were already installed. Again this is due to shoe-horned non-program

      • by jythie ( 914043 )
        This.. really gets into the other big problem with Python. The core developers, the ones working on the language, are mostly programmer/CS type people. But the whole Pandas/Numpy/Scipi community, yeah, they are datascientists and statisticians that are used to R/SAS/etc and have built a dialect of python that behaves more like their preferred languages... so they add in more and more inconsistent behavior because it 'looks right'.

He has not acquired a fortune; the fortune has acquired him. -- Bion

Working...