PrivateConstructorMeta¶
-
class
lsst.daf.butler.core.utils.
PrivateConstructorMeta
¶ Bases:
type
A metaclass that disables regular construction syntax.
A class that uses PrivateConstructorMeta may have an
__init__
and/or__new__
method, but these can’t be invoked by “calling” the class (that will always raiseTypeError
). Instead, such classes can be called by calling the metaclass-provided_construct
class method with the same arguments.As is usual in Python, there are no actual prohibitions on what code can call
_construct
; the purpose of this metaclass is just to prevent instances from being created normally when that can’t do what users would expect...note:
Classes that inherit from PrivateConstructorMeta also inherit the hidden-constructor behavior. If you just want to disable construction of the base class, `abc.ABCMeta` may be a better option.
Examples
- Given this class definition::
class Hidden(metaclass=PrivateConstructorMeta):
- def __init__(self, a, b):
self.a = a self.b = b
This doesn’t work:
>>> instance = Hidden(a=1, b="two") TypeError: Hidden objects cannot be constructed directly.
But this does:
>>> instance = Hidden._construct(a=1, b="two")
Methods Summary
__call__
(*args, **kwds)Disabled class construction interface; always raises
TypeError.
mro
($self, /)Return a type’s method resolution order.
Methods Documentation
-
__call__
(*args, **kwds)¶ Disabled class construction interface; always raises
TypeError.
-
mro
($self, /)¶ Return a type’s method resolution order.