Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Back to Docs_main.

pyd.object



class DPyObject;
Wrapper class for a Python/C API PyObject.

Nearly all of these member functions may throw a PythonException if the underlying Python API raises a Python exception.

Authors:
Kirk McDonald

Date:
June 18, 2006

See Also:
The Python/C API

this(PyObject * o, bool borrowed = false);
Wrap around a passed PyObject*.

Params:
PyObject * o The PyObject to wrap.
bool borrowed Whether o is a borrowed reference. Instances of DPyObject always own their references. Therefore, Py_INCREF will be called if borrowed is true.

this();
The default constructor constructs an instance of the Py_None DPyObject.

PyObject * ptr();
Returns a borrowed reference to the PyObject.

bool hasattr(char[] attr_name);
Same as hasattr(this, attr_name) in Python.

bool hasattr(DPyObject attr_name);
Same as hasattr(this, attr_name) in Python.

DPyObject getattr(char[] attr_name);
Same as getattr(this, attr_name) in Python.

DPyObject getattr(DPyObject attr_name);
Same as getattr(this, attr_name) in Python.

void setattr(char[] attr_name, DPyObject v);
Same as setattr(this, attr_name, v) in Python.

void setattr(DPyObject attr_name, DPyObject v);
Same as setattr(this, attr_name, v) in Python.

void delattr(char[] attr_name);
Same as del this.attr_name in Python.

void delattr(DPyObject attr_name);
Same as del this.attr_name in Python.

int opCmp(DPyObject rhs);
Exposes Python object comparison to D. Same as cmp(this, rhs) in Python.

bool opEquals(DPyObject rhs);
Exposes Python object equality check to D.

DPyObject repr();
Same as repr(this) in Python.

DPyObject str();
Same as str(this) in Python.

char[] toString();
Allows use of DPyObject in writef via %s

DPyObject unicode();
Same as unicode(this) in Python.

bool isInstance(DPyObject cls);
Same as isinstance(this, cls) in Python.

bool isSubclass(DPyObject cls);
Same as issubclass(this, cls) in Python. Only works if this is a class.

bool callable();
Same as callable(this) in Python.

DPyObject opCall(DPyObject args = cast(DPyObject)null);
Calls the DPyObject.

Params:
DPyObject args Should be a DPyTuple of the arguments to pass. Omit to call with no arguments.

Returns:
Whatever the function DPyObject returns.

DPyObject opCall(DPyObject args, DPyObject kw);
Calls the DPyObject with positional and keyword arguments.

Params:
DPyObject args Positional arguments. Should be a DPyTuple. Pass an empty DPyTuple for no positional arguments.
DPyObject kw Keyword arguments. Should be a DPyDict.

Returns:
Whatever the function DPyObject returns.

DPyObject method(char[] name, DPyObject args = cast(DPyObject)null);


int hash();
Same as hash(this) in Python.

bool toBool();
Same as "not not this" in Python.

bool not();
Same as "not this" in Python.

DPyObject type();
Gets the type of this DPyObject. Same as type(this) in Python.

Returns:
The type DPyObject of this DPyObject.

int length();
The length of this DPyObject. Same as len(this) in Python.

int size();
Same as length()

DPyObject dir();
Same as dir(this) in Python.

DPyObject opIndex(DPyObject key);
Equivalent to o[key] in Python.

DPyObject opIndex(char[] key);
Equivalent to o['key'] in Python; usually only makes sense for mappings.

DPyObject opIndex(int i);
Equivalent to o[i] in Python; usually only makes sense for sequences.

void opIndexAssign(DPyObject value, DPyObject key);
Equivalent to o[key] = value in Python.

void opIndexAssign(DPyObject value, char[] key);
Equivalent to o['key'] = value in Python. Usually only makes sense for mappings.

void opIndexAssign(DPyObject value, int i);
Equivalent to o[i] = value in Python. Usually only makes sense for sequences.

void delItem(DPyObject key);
Equivalent to del o[key] in Python.

void delItem(char[] key);
Equivalent to del o['key'] in Python. Usually only makes sense for mappings.

void delItem(int i);
Equivalent to del o[i] in Python. Usually only makes sense for sequences.

DPyObject opSlice(int i1, int i2);
Equivalent to o[i1:i2] in Python.

DPyObject opSlice();
Equivalent to o[:] in Python.

void opSliceAssign(DPyObject v, int i1, int i2);
Equivalent to o[i1:i2] = v in Python.

void opSliceAssign(DPyObject v);
Equivalent to o[:] = v in Python.

void delSlice(int i1, int i2);
Equivalent to del o[i1:i2] in Python.

void delSlice();
Equivalent to del o[:] in Python.

int opApply(int delegate(inout DPyObject) dg);
Iterates over the items in a collection, be they the items in a sequence, keys in a dictionary, or some other iteration defined for the DPyObject's type.

int opApply(int delegate(inout DPyObject, inout DPyObject) dg);
Iterate over (key, value) pairs in a dictionary. If the DPyObject is not a dict, this simply does nothing. (It iterates over no items.) You should not attempt to modify the dictionary while iterating through it, with the exception of modifying values. Adding or removing items while iterating through it is an especially bad idea.

DPyObject opAdd(DPyObject o);


DPyObject opSub(DPyObject o);


DPyObject opMul(DPyObject o);


DPyObject opMul(int count);
Sequence repetition

DPyObject opDiv(DPyObject o);


DPyObject floorDiv(DPyObject o);


DPyObject opMod(DPyObject o);


DPyObject divmod(DPyObject o);


DPyObject pow(DPyObject o1, DPyObject o2 = cast(DPyObject)null);


DPyObject opPos();


DPyObject opNeg();


DPyObject abs();


DPyObject opCom();


DPyObject opShl(DPyObject o);


DPyObject opShr(DPyObject o);


DPyObject opAnd(DPyObject o);


DPyObject opXor(DPyObject o);


DPyObject opOr(DPyObject o);


DPyObject opAddAssign(DPyObject o);


DPyObject opSubAssign(DPyObject o);


DPyObject opMulAssign(DPyObject o);


DPyObject opMulAssign(int count);
In-place sequence repetition

DPyObject opDivAssign(DPyObject o);


DPyObject floorDivAssign(DPyObject o);


DPyObject opModAssign(DPyObject o);


DPyObject powAssign(DPyObject o1, DPyObject o2 = cast(DPyObject)null);


DPyObject opShlAssign(DPyObject o);


DPyObject opShrAssign(DPyObject o);


DPyObject opAndAssign(DPyObject o);


DPyObject opXorAssign(DPyObject o);


DPyObject opOrAssign(DPyObject o);


DPyObject asInt();


DPyObject asLong();


DPyObject asFloat();


int toLong();


long toLongLong();


double toDouble();


cdouble toComplex();


DPyObject opCat(DPyObject o);
Sequence concatenation

DPyObject opCatAssign(DPyObject o);
In-place sequence concatenation

int count(DPyObject v);


int index(DPyObject v);


DPyObject asList();
Converts any iterable DPyObject to a list

DPyObject asTuple();
Converts any iterable DPyObject to a tuple

bool opIn_r(DPyObject v);
Same as "v in this" in Python.

bool hasKey(DPyObject key);
Same as opIn_r

bool opIn_r(char[] key);
Same as "'v' in this" in Python.

bool hasKey(char[] key);
Same as opIn_r

DPyObject keys();


DPyObject values();


DPyObject items();