mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Experimental feature: add default argument to getattr().
This commit is contained in:
parent
39ef2274a3
commit
950ff2923a
1 changed files with 13 additions and 5 deletions
|
@ -669,18 +669,26 @@ builtin_getattr(self, args)
|
||||||
PyObject *self;
|
PyObject *self;
|
||||||
PyObject *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
PyObject *v;
|
PyObject *v, *result, *dflt = NULL;
|
||||||
PyObject *name;
|
PyObject *name;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "OS:getattr", &v, &name))
|
if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
|
||||||
return NULL;
|
return NULL;
|
||||||
return PyObject_GetAttr(v, name);
|
result = PyObject_GetAttr(v, name);
|
||||||
|
if (result == NULL && dflt != NULL) {
|
||||||
|
PyErr_Clear();
|
||||||
|
Py_INCREF(dflt);
|
||||||
|
result = dflt;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char getattr_doc[] =
|
static char getattr_doc[] =
|
||||||
"getattr(object, name) -> value\n\
|
"getattr(object, name[, default]) -> value\n\
|
||||||
\n\
|
\n\
|
||||||
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.";
|
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
|
||||||
|
When a default argument is given, it is returned when the attribute doesn't\n\
|
||||||
|
exist; without it, an exception is raised in that case.";
|
||||||
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue