.. _named-tuple-factory:
-:func:`NamedTuple` factory function
------------------------------------
+:func:`NamedTuple` Factory Function for Tuples with Named Fields
+----------------------------------------------------------------
Named tuples assign meaning to each position in a tuple and allow for more readable,
self-documenting code. They can be used wherever regular tuples are used, and
method which lists the tuple contents in a ``name=value`` format.
The *fieldnames* are specified in a single string with each fieldname separated by
- a space and/or comma. Any valid Python identifier may be used for a field name.
+ a space and/or comma. Any valid Python identifier may be used for a fieldname.
- If *verbose* is true, the *NamedTuple* call will print the class definition.
+ If *verbose* is true, will print the class definition.
*NamedTuple* instances do not have per-instance dictionaries, so they are
- lightweight, requiring no more memory than regular tuples.
+ lightweight and require no more memory than regular tuples.
Example::
.. method:: somenamedtuple.replace(field, value)
- Return a new instance of the named tuple replacing the named *field* with a new *value*::
+ Return a new instance of the named tuple replacing the named *field* with a new *value*:
+
+::
>>> p = Point(x=11, y=22)
>>> p.__replace__('x', 33)
Return a tuple of strings listing the field names. This is useful for introspection,
for converting a named tuple instance to a dictionary, and for combining named tuple
- types to create new named tuple types::
+ types to create new named tuple types:
+
+::
>>> p.__fields__ # view the field names
('x', 'y')
typedef unsigned long long uint64;
#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
- section should work for GCC on any PowerPC platform,
- irrespective of OS. POWER? Who knows :-) */
+ section should work for GCC on any PowerPC
+ platform, irrespective of OS.
+ POWER? Who knows :-) */
#define READ_TIMESTAMP(var) ppc_getcounter(&var)
static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
static PyObject * do_call(PyObject *, PyObject ***, int, int);
static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
-static PyObject * update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
+static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
+ PyObject *);
static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
static PyObject * load_args(PyObject ***, int);
#define CALL_FLAG_VAR 1
PyObject *
PyEval_EvalFrame(PyFrameObject *f) {
/* This is for backward compatibility with extension modules that
- used this API; core interpreter code should call PyEval_EvalFrameEx() */
+ used this API; core interpreter code should call
+ PyEval_EvalFrameEx() */
return PyEval_EvalFrameEx(f, 0);
}
#ifdef DXPAIRS
int lastopcode = 0;
#endif
- register PyObject **stack_pointer; /* Next free slot in value stack */
+ register PyObject **stack_pointer; /* Next free slot in value stack */
register unsigned char *next_instr;
register int opcode; /* Current opcode */
register int oparg; /* Current opcode argument, if any */
#define JUMPBY(x) (next_instr += (x))
/* OpCode prediction macros
- Some opcodes tend to come in pairs thus making it possible to predict
- the second code when the first is run. For example, COMPARE_OP is often
- followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
- followed by a POP_TOP.
+ Some opcodes tend to come in pairs thus making it possible to
+ predict the second code when the first is run. For example,
+ COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
+ those opcodes are often followed by a POP_TOP.
Verifying the prediction costs a single high-speed test of register
variable against a constant. If the pairing was good, then the
#define PUSH(v) { (void)(BASIC_PUSH(v), \
lltrace && prtrace(TOP(), "push")); \
assert(STACK_LEVEL() <= co->co_stacksize); }
-#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
+#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
+ BASIC_POP())
#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
lltrace && prtrace(TOP(), "stackadj")); \
assert(STACK_LEVEL() <= co->co_stacksize); }
-#define EXT_POP(STACK_POINTER) (lltrace && prtrace((STACK_POINTER)[-1], "ext_pop"), *--(STACK_POINTER))
+#define EXT_POP(STACK_POINTER) (lltrace && prtrace((STACK_POINTER)[-1], \
+ "ext_pop"), *--(STACK_POINTER))
#else
#define PUSH(v) BASIC_PUSH(v)
#define POP() BASIC_POP()
if ((x = f->f_locals) != NULL) {
if ((err = PyObject_DelItem(x, w)) != 0)
format_exc_check_arg(PyExc_NameError,
- NAME_ERROR_MSG ,w);
+ NAME_ERROR_MSG,
+ w);
break;
}
PyErr_Format(PyExc_SystemError,
PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
case UNPACK_SEQUENCE:
v = POP();
- if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
- PyObject **items = ((PyTupleObject *)v)->ob_item;
+ if (PyTuple_CheckExact(v) &&
+ PyTuple_GET_SIZE(v) == oparg) {
+ PyObject **items = \
+ ((PyTupleObject *)v)->ob_item;
while (oparg--) {
w = items[oparg];
Py_INCREF(w);
}
Py_DECREF(v);
continue;
- } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
- PyObject **items = ((PyListObject *)v)->ob_item;
+ } else if (PyList_CheckExact(v) &&
+ PyList_GET_SIZE(v) == oparg) {
+ PyObject **items = \
+ ((PyListObject *)v)->ob_item;
while (oparg--) {
w = items[oparg];
Py_INCREF(w);
PUSH(w);
}
} else if (unpack_iterable(v, oparg, -1,
- stack_pointer + oparg)) {
+ stack_pointer + oparg)) {
stack_pointer += oparg;
} else {
/* unpack_iterable() raised an exception */
else {
x = PyObject_GetItem(v, w);
if (x == NULL && PyErr_Occurred()) {
- if (!PyErr_ExceptionMatches(PyExc_KeyError))
+ if (!PyErr_ExceptionMatches(
+ PyExc_KeyError))
break;
PyErr_Clear();
}
if (x == NULL) {
format_exc_check_arg(
PyExc_NameError,
- NAME_ERROR_MSG ,w);
+ NAME_ERROR_MSG, w);
break;
}
}
UNBOUNDLOCAL_ERROR_MSG,
v);
} else {
- v = PyTuple_GET_ITEM(
- co->co_freevars,
- oparg - PyTuple_GET_SIZE(co->co_cellvars));
- format_exc_check_arg(
- PyExc_NameError,
- UNBOUNDFREE_ERROR_MSG,
- v);
+ v = PyTuple_GET_ITEM(co->co_freevars, oparg -
+ PyTuple_GET_SIZE(co->co_cellvars));
+ format_exc_check_arg(PyExc_NameError,
+ UNBOUNDFREE_ERROR_MSG, v);
}
break;
continue;
}
if (PyErr_Occurred()) {
- if (!PyErr_ExceptionMatches(PyExc_StopIteration))
+ if (!PyErr_ExceptionMatches(
+ PyExc_StopIteration))
break;
PyErr_Clear();
}
case SETUP_LOOP:
case SETUP_EXCEPT:
case SETUP_FINALLY:
- /* NOTE: If you add any new block-setup opcodes that are
- not try/except/finally handlers, you may need to
- update the PyGen_NeedsFinalizing() function. */
+ /* NOTE: If you add any new block-setup opcodes that
+ are not try/except/finally handlers, you may need
+ to update the PyGen_NeedsFinalizing() function.
+ */
PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
STACK_LEVEL());
if (v->ob_refcnt == 2) {
/* In the common case, there are 2 references to the value
* stored in 'variable' when the += is performed: one on the
- * value stack (in 'v') and one still stored in the 'variable'.
- * We try to delete the variable now to reduce the refcnt to 1.
+ * value stack (in 'v') and one still stored in the
+ * 'variable'. We try to delete the variable now to reduce
+ * the refcnt to 1.
*/
switch (*next_instr) {
case STORE_FAST:
}
case STORE_DEREF:
{
- PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
+ PyObject **freevars = (f->f_localsplus +
+ f->f_code->co_nlocals);
PyObject *c = freevars[PEEKARG()];
if (PyCell_GET(c) == v)
PyCell_Set(c, NULL);
*/
if (_PyString_Resize(&v, new_len) != 0) {
/* XXX if _PyString_Resize() fails, 'v' has been
- * deallocated so it cannot be put back into 'variable'.
- * The MemoryError is raised when there is no value in
- * 'variable', which might (very remotely) be a cause
- * of incompatibilities.
+ * deallocated so it cannot be put back into
+ * 'variable'. The MemoryError is raised when there
+ * is no value in 'variable', which might (very
+ * remotely) be a cause of incompatibilities.
*/
return NULL;
}