UserSpace.relref#
- UserSpace.relref(**kwargs)[source]#
Set references in relative mode.
Creates references that adjust their bindings based on inheritance context. When a space with relative references is inherited, the references are resolved relative to the derived space’s position in the hierarchy. The mode of a reference is relevant only when modelx objects, such as cells or spaces, are assigned to the reference.
This is useful for creating reusable space templates where references should point to “local” versions of objects in derived contexts.
This is a convenience method equivalent to calling
set_ref()withrefmode="relative"for each reference.Relative vs Absolute:
Relative: Reference adjusts based on inheritance context
Absolute: Reference always points to the same object
- Parameters:
**kwargs – Keyword arguments where each key is the reference name and each value is the object to reference. Multiple references can be set in a single call.
Example
In the example below, a cells
foois defined in SpaceS.abs_fooin SpaceSis defined as an absolute reference tofoo, whilerel_foois defined as a relative reference.When Space
Sis inherited byS[y],abs_foocontinues to point toS.foo, whilerel_foopoints toS[y].foo, which uses the parameteryin stead ofS.y:>>> import modelx as mx >>> s = mx.new_space('S') >>> @mx.defcells ... def foo(x): ... return x + y >>> s.absref(abs_foo=foo) >>> s.relref(rel_foo=foo) >>> s.y = 0 >>> s.parameters = 'y' >>> s[1].abs_foo(10) # Absolute reference to S.foo 10 >>> s[1].rel_foo(10) # Relative reference to S[1].foo 11