Skip to content

Remove shared_parameters? #183

Description

@ceball

Docstring:

Context manager to share parameter instances when creating multiple Parameterized objects of the same type. Parameter default values are instantiated once and cached to be reused when another Parameterized object of the same type is instantiated. Can be useful to easily modify large collections of Parameterized objects at once and can provide a significant speedup.

Seems like the intention there could already be achieved without shared_parameters:

import param

class A(param.Parameterized):
    x = param.List(default=[1,2], instantiate=False)

instances = [A() for _ in range(2)]

# modify all instances together
A.x.append(3)

for i in instances:
    print(i.x)

However, Philipp pointed out:

the point was to automatically speed up the creation of a lot of near-identical objects without having to set instantiate=False in the declaration of the parameter

Again, though, that could be achieved by setting A.params('x').instantiate = False while creating the instances.

However, shared_parameters do give a way to modify all instance parameters without also modifying the default value (i.e. that on the class). So the example above should be more like this:

import param

class A(param.Parameterized):
    x = param.List(default=[1,2])

A.params('x').instantiate = False
old_default = A.x
A.x = deepcopy(A.x) # instantiate
instances = [A() for _ in range(2)]
A.params('x').instantiate = True
A.x = old_default

# modify all instances together
a.x.append(3)

for i in instances:
    print(i.x)

So I think that deciding about keeping or removing shared_parameters is deciding if the above modify-all-instances-without-modifying-the-class/default is useful. If keeping, I think the current shared_parameters implementation could probably be simplified (e.g. using a context manager to implement something like the above). I haven't thought much about that, though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API breakingAffects the api in a non bug fixy way/consider version or name changestatus: discussionDiscussion. Not yet a specific feature/bug. Likely to result in multiple PRs/issues.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions