MCPcopy Create free account
hub / github.com/ipython/traitlets / default

Function default

traitlets/traitlets.py:1200–1238  ·  view source on GitHub ↗

A decorator which assigns a dynamic default for a Trait on a HasTraits object. Parameters ---------- name The str name of the Trait on the object whose default should be generated. Notes ----- Unlike observers and validators which are properties of the HasTraits

(name: str)

Source from the content-addressed store, hash-verified

1198
1199
1200def default(name: str) -> DefaultHandler:
1201 """A decorator which assigns a dynamic default for a Trait on a HasTraits object.
1202
1203 Parameters
1204 ----------
1205 name
1206 The str name of the Trait on the object whose default should be generated.
1207
1208 Notes
1209 -----
1210 Unlike observers and validators which are properties of the HasTraits
1211 instance, default value generators are class-level properties.
1212
1213 Besides, default generators are only invoked if they are registered in
1214 subclasses of `this_type`.
1215
1216 ::
1217
1218 class A(HasTraits):
1219 bar = Int()
1220
1221 @default('bar')
1222 def get_bar_default(self):
1223 return 11
1224
1225 class B(A):
1226 bar = Float() # This trait ignores the default generator defined in
1227 # the base class A
1228
1229 class C(B):
1230
1231 @default('bar')
1232 def some_other_default(self): # This default generator should not be
1233 return 3.0 # ignored since it is defined in a
1234 # class derived from B.a.this_class.
1235 """
1236 if not isinstance(name, str):
1237 raise TypeError("Trait name must be a string or All, not %r" % name)
1238 return DefaultHandler(name)
1239
1240
1241FuncT = t.TypeVar("FuncT", bound=t.Callable[..., t.Any])

Callers

nothing calls this directly

Calls 1

DefaultHandlerClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…