1. ariovistus
  2. pyd

Wiki

Clone wiki

pyd / Inheritance

Inheritance

If you wrap both a class and a child of that class, Pyd is smart enough to make the resulting Python classes have a parent-child relationship. Any methods of the parent class will automatically be available to the child class. If the child class overloads any of those methods, it is important that the user wrap them in the module's init function. For example:

import std.stdio;

class Base {
    void foo() { writefln("Base.foo"); }
    void bar() { writefln("Base.bar"); }
}

class Derived : Base {
    void foo() { writefln("Derived.foo"); }
}

These would be exposed to Python by putting this code in PydMain after the call to module_init:

wrap_class!(
    Base,
    Def!(Base.foo),
    Def!(Base.bar),
);

wrap_class!(
    Derived,
    Def!(Derived.foo),
);

When used in Python, we get the expected behavior:

>>> issubclass(Derived, Base)
True
>>> b = Base()
>>> d = Derived()
>>> b.foo()
Base.foo
>>> b.bar()
Base.bar
>>> d.foo()
Derived.foo
>>> d.bar()
Base.bar

Polymorphic behavior is also automatically taken care of. Take a function like the following:

void polymorphic_call(Base b) {
    b.foo();
}

And in Python:

>>> class PyClass(Base):
... 	def foo(self):
... 		print "PyClass.foo"
... 
>>> p = PyClass()
>>> polymorphic_call(p)
PyClass.foo

interfaces? abstract classes? wut?

Updated