Python (programming language): Difference between revisions
imported>Kirby Urner m (→Origins: slight code improvements) |
mNo edit summary |
||
Line 126: | Line 126: | ||
==Notes and references== | ==Notes and references== | ||
{{reflist}} | {{reflist}}[[Category:Suggestion Bot Tag]] |
Latest revision as of 16:00, 8 October 2024
Authors [about]:
join in to develop this article! |
Python is a dynamic object-oriented, general purpose interpreted programming language.
Origins
Python, the computer language, was first designed and implemented on an Apple Lisa, a forerunner of the more reasonably priced Macintosh. Written in the C language, by Guido van Rossum, Python was meant as a next generation ABC, the name of a shell-based interactive language, used in house at Stichting Mathematisch Centrum (Amsterdam), the think tank where Guido, a Dutchman, then worked (circa 1991).
Python, like ABC before it, was created for busy professionals already well versed in technical disciplines and needing to avail themselves of computing power without detouring into computer science for yet another degree. The Python language “fits your brain” (gets out of the way) and comes with “batteries included” (lots of libraries).
That the language could live up to its own hype is testament to Guido’s strong sense of design and resistance to cruft (unnecessary complexity). The language has only two looping constructs for example, based on keywords while and for. Coders extend the built-in type system using the keyword class to define new types.
“Computer Programming for Everybody” (CP4E) was Guido’s rallying cry, later the title of his successful DARPA proposal, but he wasn’t reinventing BASIC. Python would be architected from the ground up to be object oriented and state of the art.
class Snake: """ Defining a type: a snake with a name and stomach """ def __init__(self, name): self.name = name self.stomach = [ ] # empty at birth def eat(self, food): self.stomach.append(food) # any_snake("🐹") synonymous with any_snake.eat("🐹") def __call__(self, food): self.eat(food) def __repr__(self): return f"Snake named {self.name} at {id(self)}"
Like Java, Python would compile from source code to an interim bytecode language that would run on a virtual machine. Although strongly typed, name bindings could change at runtime, meaning no type declarations up front, making Python a (very) high level language, and therefore slower and more memory hungry than languages with tinier runtimes, such as Rust. Python includes runtime garbage collecting of no longer used objects, and dynamically allocates more memory as objects require.
More pared-down faster relatives of Python, such as Cython, come down elsewhere along the flexibility, to runtime speed, tradeoff.
Unlike in Java, free-standing function objects (outside any class) are allowed, and may be passed around as arguments, as in JavaScript.
Also unlike Java, but like C++, operator overloading (changing the meaning of arithmetic symbols) is supported.
Even the behavior of brackets and parentheses — e.g. dog[] and cat() — may be altered on a per type basis, by providing code for the corresponding “special method names” such as: __add__ for +; __mul__ for *; __getitem__ and __setitem__ for brackets; and __call__ for parentheses.
Python has about fifty of such builtin “__ribs__” (i.e. special names). Mnemonic: “a snake has lots of __ribs__” (also known as “magic methods”).
The conversation below, at the Python REPL (read evaluate print loop), or shell, makes use of the Snake type defined above. Calling the type (Snake) triggers the constructor (__init__), with Python supplying the leftmost argument (self). Once an instance is defined (e.g. naga), directly calling it (an optional feature) triggers the __call__ method, which in turn invokes the eat method.
>>> any_snake = Snake("Naga") # triggers __init__ >>> any_snake("🐹") # triggers __call__ >>> any_snake.eat("snack") # appends to stomach >>> any_snake.stomach # accessing an attribute ['🐹', 'snack'] >>> any_snake # triggers __repr__ Snake named Naga at 4634375632 >>> another_snake = Snake("Twila") >>> another_snake Snake named Twila at 4604937040
Early Success
Python never billed itself as a “teaching language” or a “beginner language” and yet ended up fitting these billings inadvertently, because it looked like “runnable pseudo-code” (clear and compact).
MIT and other top universities gradually gave it a more prominent position in their curricula, much to the dismay of purists who had grown up learning LISP, and thereby contributing to growing resentment in some corners. The early success of Python may be attributed to two factors:
- it rode the wave of the free and open source movement, meaning it enjoyed partaking of the most creative software development ecosystem ever devised and
- it ran well on Windows, itself proprietary, but ubiquitous, and likewise a platform for free open software development.
Python’s stash of third party contributed packages, for game development, cryptography, physics modeling, and bioinformatics, exploded exponentially. In Python, as in Wolfram Language (Mathematica), scientists were discovering a cross-disciplinary lingua franca they could all develop in common.
Not only did it run on Windows in a GUI-based development environment, but the very same IDE (named IDLE, a pun on Eric Idle of Monty Python fame) ran on OSX and Linux as well.
DARPA’s funding of the CP4E proposal had given Guido time to develop IDLE, a hybrid project wherein the graphical widgets came from Tk (toolkit) written in tcl (tickle) but with Python bindings via Tkinter. To this day, Python leaves it to 3rd party libraries to provide GUI bindings (wxPython, PyQT, PyGTK…) to all but Tk (Tkinter is in the Standard Library).
SQLAlchemy provided a sophisticated object relational mapper (ORM). Twisted supported all manner of TCP/IP protocols, while the WSGI standard allowed for all manner of web serving frameworks.
Numpy and Scipy, enjoying a boost from the astronomy community especially, added more significant number crunching (e.g. linear algebra) and other science-related algorithms to Python’s ecosystem.
Python became one of the “P languages” of the LAMP stack, joining PHP and Perl atop MySQL, Apache, and Linux. Many more stacks (too many to discuss here) were to follow.
The Great Leap Forward
Although already embraced by industry and ramping up as an inhouse favorite at many companies, Google especially (because of its ability to wrap C and C++ for interactive dynamic use), Guido came to realize his early draft had some ugly “warts” he should address. This entailed making some breaking changes that would make future Python source incompatible with the then-current version 2.x interpreter.
Already successful languages usually try to avoid upsetting the applecart, endangering their privileged position, but Guido, by then affectionately referred to as Benevolent Dictator for Life, announced these breaking changes would be coming with the move to Python 3.
Y2K hysteria was already in the air and somewhat in parody, in true Monty Python fashion, the Python Community touted Python 3000 as a part of the coming millennial apocalypse.
1/2 would give a floating point answer (0.5) instead of an integer one (0). Use 1//2 instead for the latter. The print keyword would now be a builtin callable function (none of the keywords are callable). More deeply under the hood, more memory-light “just in time” constructs were introduced (e.g. range and zip), and programmed classes were more tightly unified with the pre-existing type system. Unicode support was more deeply integrated.
The language became more elegant and streamlined, laying a stronger foundation for future growth. Guido’s bold plans had paid off.
With its warts now addressed, Python 3 would go on to take the world by storm. The libraries became more powerful, and the 3rd party ecosystem ballooned with free frameworks.
In truth, Python web server solutions at first suffered from the bewildering variety of options, a disorienting cornucopia, such that Ruby on Rails (Ruby is a language more like Perl) began pulling ahead as an eclipsing technology with a stronger community. Community matters.
Python’s apparent saviors were Flask and Django, two high profile web serving solutions spanning the spectrum from micro to macro framework, in terms of infrastructure provided, from minimalist / expandable to fully loaded out of the box.
Around each project, a unique community developed. For example, Django grew out of a newspaper publishing community in Lawrence, Kansas, taking deadlines and documentation seriously and attracting an international league of top developers. Django jobs proliferated. Djangocon eventually abetted EuroPython and Pycon on the annual schedule of roaming conferences.
Additional examples of “Pythonista subcultures” would be the Blender (computer generated imagery) and ESRI (geographical information systems) communities. Both products, open and closed source respectively, are animated from within (scripted) using Python.
Python Today
By 2020, WSGI (Web Server Gateway Interface) was being abetted by ASGI (Asynchronous Server Gateway Interface), taking advantage of Python’s newest feature: the ability to define event loops of asynchronous tasks (coroutines) that may be nudged forward concurrently, in place of more complicated threading patterns.
HTTP / HTTPS has been enhanced with Websockets, a newer bidirectional web-oriented protocol, which ASGI likewise supports. The Django community was at the core of this effort, to keep Python abreast these latest changes in web server and API design. Where Python really took off after around 2015 was in data science, owing to two trends:
1. Jupyter Notebook technology (oft rebranded), evolving from I-Python, itself a more developed interactive console environment than IDLE’s. Jupyter is incorporated into the popular Anaconda distribution, with DARPA funding again.
Jupyter Notebooks implement what Donald Knuth termed “literate programming” in that code cells intermix with “markdown cells” containing formatted text, graphics, hyperlinks. Other languages besides Python may run in code cells (“Jupyter” is from Julia, Python and R — early adopters). The Atlantic Monthly (April 5, 2018) predicted Jupyter Notebook technology would eventually revolutionize scientific publishing, as Notebooks encapsulate relevant runnable code in a publication-quality format.
2. the Machine Learning revolution, linked to the evolution of GPUs as generic number crunching devices, and of Python APIs to these new predictive data-fitting models. Google would introduce TensorFlow, whereas Facebook contributed the competing Pytorch. Scikit-learn provided an additional on ramp, complete with its own teaching materials.
Python was now competing with both Matlab and R, both inner sanctum graduate school tools not used to much competition.
Now seen as a “King of the Hill” in some ways, many languages would portray themselves as “the next Python” or the “Python killer” (shades of Greek mythology) as a way to gain traction and capitalize on some feelings of resentment and jealousy in light of Python’s evident success.
In the meantime, Guido encountered huge pushback gaining acceptance for a new assignment operator, the so-called walrus whiskers :=, and after achieving victory, announced that the language must be in good hands, since even as dictator he was barely able to make additional changes.
He took that as a sign of community maturity and demoted himself to exBDFL. He continued to work on the language however, and to play a mentoring role.