holoviz / param

Param: Make your Python code clearer and more reliable by declaring Parameters
https://param.holoviz.org
BSD 3-Clause "New" or "Revised" License
422 stars 73 forks source link

Remove shared_parameters? #183

Open ceball opened 7 years ago

ceball commented 7 years ago

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.

tonyfast commented 4 years ago

Consider removing this for 2.0. Chime in if you object.

maximlt commented 1 year ago

shared_parameters wasn't deprecated in 1.x and was even documented. It will have to stay a little longer.