background preloader

Python in depth

Facebook Twitter

Here be links to articles about the innards of Python implementations.

Python 3's range is more powerful than Python 2's xrange - Trey Hunner. If you’re switching between Python 2 and Python 3, you might think that Python 2’s xrange objects are pretty much the identical to Python 3’s range object.

Python 3's range is more powerful than Python 2's xrange - Trey Hunner

It seems like they probably just renamed xrange to range, right? Not quite. Python 2’s xrange is somewhat more limited than Python 3’s range. Functional Programming HOWTO — Python 3.9.0 documentation. Author A.

Functional Programming HOWTO — Python 3.9.0 documentation

M. Kuchling Release In this document, we’ll take a tour of Python’s features suitable for implementing programs in a functional style. Introduction This section explains the basic concept of functional programming; if you’re just interested in learning about Python language features, skip to the next section on Iterators. Programming languages support decomposing problems in several different ways: Most programming languages are procedural: programs are lists of instructions that tell the computer what to do with the program’s input. The designers of some computer languages choose to emphasize one particular approach to programming.

In a functional program, input flows through a set of functions. Some languages are very strict about purity and don’t even have assignment statements such as a=3 or c = a + b, but it’s difficult to avoid all side effects. Functional programming can be considered the opposite of object-oriented programming. Formal provability Modularity Composability. Iterators, generators and decorators — Python for you and me 0.4.beta1 documentation. In this chapter we will learn about iterators, generators and decorators.

Iterators, generators and decorators — Python for you and me 0.4.beta1 documentation

Iterators Python iterator objects are required to support two methods while following the iterator protocol. __iter__ returns the iterator object itself. This is used in for and in statements. __next__ method returns the next value from the iterator. Class Counter(object): def __init__(self, low, high): self.current = low self.high = high def __iter__(self): 'Returns itself as an iterator object' return self def __next__(self): 'Returns the next value till current is lower than high' if self.current > self.high: raise StopIteration else: self.current += 1 return self.current - 1 Now we can use this iterator in our code. >>> c = Counter(5,10)>>> for i in c:... print(i, end=' ')...5 6 7 8 9 10 Remember that an iterator object can be used only once. Using the iterator in for loop example we saw, the following example tries to show the code behind the scenes. 2 great benefits of Python generators (and how they changed me forever) – O’Reilly.

I had no idea how important it would turn out to be.

2 great benefits of Python generators (and how they changed me forever) – O’Reilly

It changed the way I wrote software forever, in every language. By “it,” I mean a Python construct called generators. It’s hard to convey everything they can do for you in a few words; you’ll have to keep reading to find out. In a nutshell: they help your program efficiently scale, while at the same time providing delightful encapsulation patterns. Let me explain. Learn faster. A generator looks a lot like a function, but uses the keyword “yield” instead of “return” (you can skip ahead a bit if you’re already familiar): def gen_nums(): n = 0 while n < 4: yield n n += 1 That’s a generator function.

>>> nums = gen_nums()>>> type(nums)<class 'generator'> A generator object is a Python iterator, so you can use it in a for loop, like this: >>> for num in nums:... print(num)0123 (Notice how state is encapsulated within the body of the generator function. A for-loop effectively calls this each time through the loop, to get the next value. Architecture of Python Virtual Machine · EOF. Python virtual machine is the core part of this language.

Architecture of Python Virtual Machine · EOF

After compiling the original python code into Opcode(byte code), python VM will take the job left. Python will take every opcode from PyCodeObject. Executing environment in Python VM Actually, all the things that VM do is simulating what the OS do to excute a program. Your Guide to the CPython Source Code. Are there certain parts of Python that just seem magic?

Your Guide to the CPython Source Code

Like how are dictionaries so much faster than looping over a list to find an item. How does a generator remember the state of the variables each time it yields a value and why do you never have to allocate memory like other languages? It turns out, CPython, the most popular Python runtime is written in human-readable C and Python code. This tutorial will walk you through the CPython source code. You’ll cover all the concepts behind the internals of CPython, how they work and visual explanations as you go. You’ll learn how to: Read and navigate the source codeCompile CPython from source codeNavigate and comprehend the inner workings of concepts like lists, dictionaries, and generatorsRun the test suiteModify or upgrade components of the CPython library to contribute them to future versions. Read Inside The Python Virtual Machine. 1.

Read Inside The Python Virtual Machine