Python Programming Language (DOS) Flashcards
Python programming language keywords, syntax, and features.
Attribute
Values associated with an individual object. Attributes are accessed using the ‘dot syntax’: a.x means fetch the x attribute from the ‘a’ object.
BDFL
Acronym for Benevolent Dictator For Life” ? a.k.a. Guido van Rossum, Python’s primary creator, figurehead and decision?maker.”
byte code
The internal representation of a Python program in the interpreter. The byte code is also cached in .pyc and .pyo files so that executing the same file is faster the second time (the step of compilation from source to byte code can be saved). This intermediate language” is said to run on a “virtual machine” that calls the subroutines corresponding to each bytecode.”
class
A template for creating user?defined objects. Class definitions normally contain method definitions that operate on instances of the class.
coercion
The implicit conversion of an instance of one type to another during an operation which involves two arguments of the same type. For example, int(3.15) converts the floating point number to the integer, 3, but in 3 + 4.5, each argument is of a different type (one int, one float), and both must be converted to the same type before they can be added or it will raise a TypeError. Coercion between two operands can be implicitly invoked with the coerce builtin function; thus, 3 + 4.5 is equivalent to operator.add(*coerce(3, 4.5)) and results in operator.add(3.0, 4.5) which is of course 7.5. Without coercion, all arguments of even compatible types would have to be normalized to the same value by the programmer, e.g., float(3) + 4.5 rather than just 3 + 4.5.
complex number
An extension of the familiar real number system in which all numbers are expressed as a sum of a real part and an imagary part. Imaginary numbers are real multiples of the imaginary unit, often written i in mathematics or j in engineering. Python has builtin support for complex numbers, which are written with this latter notation; the imaginary part is written with a j suffix, e.g., 3+1j. To get access to complex equivalents of the math module, use cmath. Use of complex numbers is a fairy advanced mathematical feature; if you’re not aware of a need for complex numbers, it’s almost certain you can safely ignore them.
conversion
The invocation of a well?defined mechanism from of transforming an instance of one type of object to an instance of another; for example, int(‘3’) will convert a string (‘3’) to an int (3).
decorator
A function that modifies another function or method. Its return value is typically a callable object, possibly the original function, but most often another function that modifies the original function’s behavior in some fashion.
descriptor
Any object that defines the methods __get__(), __set__(), or __delete__(). When a class attribute is a descriptor, its special binding behavior is triggered upon attribute lookup. Normally, writing a.b looks up the object b in the class dictionary for a, but if b is a descriptor, the defined method gets called. Understanding descriptors is a key to a deep understanding of Python because they are the basis for many features including functions, methods,properties, class methods, static methods, and reference to super classes.
dictionary
A built?in Python data type composed of arbitrary keys and values; sometimes called a hash” or a “hash map” in other languages, although this is technically a misnomer (hashing is one way to implement an associative array but not the only way). The use of dict much resembles that for list, but the keys can be any object with a __hash__ function, not just integers starting from zero. Examples: d = {‘A’:65, ‘B’:66}, d = dict([(‘A’, 65), (‘B’, 66)]), d[‘C’] = 67”
docstring
A string that appears as the lexically first expression in a module, class definition or function/method definition is assigned as the __doc__ attribute of the object where it is available to documentation tools or the help() builtin function.
duck typing
From the If it walks, talks, and looks like a duck, then its a duck” principle. Python uses duck typing in that if an object of some user?defined type exhibits all of the expected interfaces of some type (say the string type), then the object can be treated as if it really were of that type.”
dynamic typing
A style of typing of variables where the type of objects to which variables are assigned can be changed merely by reassigning the variables. Python is dynamically typed. Thus, unlike as in a statically typed language such as C, a variable can first be assigned a string, then an integer, and later a list, just by making the appropriate assignment statements. This frees the programmer from managing many details, but does come at a performance cost.
EAFP
Acronym for the saying it’s Easier to Ask for Forgiveness than Permission”. This common Python coding style assumes the existance of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statments. The technique contrasts with the LBYL style that is common in many other languages such as C.”
EIBTI
Acronym for Explicit Is Better Than Implicit”, one of Python’s design principles, included in the Zen of Python.”
Exception
???
first?class object
A first class object in a programming language is a language object that can be treated dynamically, stored in a variable, passed as a parameter to a function and returned as a result by a function. In Python, practically all objects are first?class, including functions, types, and classes.
function
A block of code that is invoked by a calling” program, best used to provide an autonomous service or calculation.”
generator function
A function that returns a generator iterator. Its definition looks like a normal function definition except that it uses the keyword YIELD. Generator functions often contain one or more FOR or WHILE loops that YIELD elements. The function execution is stopped at the YIELD keyword (returning the result) and its resumed there when the next element is requested (e.g., by the built in function NEXT( ) ). For details, see PEP 0255 and PEP 0342.
generator
The common name for a generator ITERATOR. The type of iterator returned by a generator function or a generator expression.
global interpreter lock or GIL
The lock used by Python threads to assure that only one thread can be run at a time. This simplifies Python by assuring that no two processes can access the same memory at the same time. Locking the entire interpreter makes it easier for the interpreter to to be multi?threaded, at the expense of some parallelism on multi?processor machines. Efforts have been made in the past to create a free?threaded” interpreter (one which locks shared data at a much finer granularity), but performance suffered in the common single?processor case.”
greedy regular expressions
Regular expressions which match the longest string possible. The * , + and ? operators are all greedy. Their counterparts *?, +? and ?? are all non?greedy (match the shortest string possible).
hash
A number used to correspond to objects, usually used for ‘hashing’ keys for storage in a hash table. Hashing in Python is done with the built?in HASH function.
hash table
An object that maps more or less arbitrary keys to values. Dictionaries are the most visible and widely used objects that exhibit this behavior.
hashable
An object is hashable if it is immutable (ints, floats, tuples, strings, etc) or user?defined classes that define a __hash__ method.