Python, being dynamically typed, has no provision for type-hinting on method parameters. However, PHP, also being dynamically typed, does have a provision for type-hinting that a method parameter is at least an instance of a class (or that it is an instance of a class that inherits from a defined class).
public class Foo()
{
public function __construct($qux, ...)
{
$this->qux = qux;
...
}
}
public class Bar()
{
// "Type Hinting" done here enforcing
// that $baz is an instance of Foo
public function __construct(Foo $baz, ...)
{
$this->baz = $baz;
...
}
}
Is there a similar way of enforcing that a method param is a specific instance in Python?
If not, is the proper convention to simply assert?
class Foo(object):
def __init__(self, qux=None, ...):
self.qux = qux
...
class Bar(object):
def __init__(self, baz=None, ...):
# "Type Hinting" done here as `assert`, and
# requires catch of AssertionError elsewhere
assert isinstance(baz, Foo)
self.baz = baz
...
If this is style of using assert
is incorrect/inelegant/"not pythonic", what should I do instead?