Get widget by name unexpected behavior in panel.param.Param class?

I was trying to get a handle on the widgets created from a Parameterized class:

import panel as pn
import param

class Test(param.Parameterized):
    p1 = param.List([1,2,3])

t = Test()
w = pn.Param(t.param)

Naturally one would want to be able to refer to a widget by name, where the widget() method seems to be designed to do.

However, the widget returned from the method is not the original widget!

>>> print(id(w.widget('p1')))
140142671301936
>>> print(id(w[1]))
140142671331792

The alternative, which is to grab the widget by position, feels fragile. So I’m still looking for a way to get widgets by name.

(This is a bit surprising to me and took me a long while to realize. I might go through its source code later to figure out what’s going on. But I wanted to do a short post to share what got me.)

not sure it’s recommanded but widgets are stored in _widgets attributes
the widget method create a new widget for the parameter
image

2 Likes