switch

paddle.fluid.unique_name.switch(new_generator=None)[source]

Switch the namespace of in current context to a new namespace. Though switch() and guard() can both change namespace, guard() is recommended since it can manage the context better together with with statement.

Parameters

new_generator (UniqueNameGenerator, optional) – A new UniqueNameGenerator, not required normally. Default is None, which means switch to a new anonymous namespace.

Returns

The previous UniqueNameGenerator.

Return type

UniqueNameGenerator

Examples

import paddle.fluid as fluid
name1 = fluid.unique_name.generate('fc')
name2 = fluid.unique_name.generate('fc')
print(name1, name2) # fc_0, fc_1

pre_generator = fluid.unique_name.switch() # switch to a new anonymous namespace.
name2 = fluid.unique_name.generate('fc')
print(name2) # fc_0

fluid.unique_name.switch(pre_generator) # switch back to pre_generator.
name3 = fluid.unique_name.generate('fc')
print(name3) # fc_2, since pre_generator has generated fc_0, fc_1.