param icon indicating copy to clipboard operation
param copied to clipboard

Class attribute inheritance deviation

Open maximlt opened this issue 3 years ago • 3 comments

In a hierarchy of classes having class attributes, updating the class attribute in a super class is seen by the subclasses. Below B.p resolves to the updated value of p on A.

class A:
    p = 1

class B(A):
    pass

print(B.p)
# 1

A.p = 2

print(B.p)
# 2

This is not how Param behaves, updating p on A has no effect on the subclasses. Not sure to which extent this is a bug.

import param

class A(param.Parameterized):
    p = param.Parameter(1)
    
class B(A):
    p = param.Parameter()

print(B.p)
# 1

A.p = 2

print(B.p)
# (Different behavior)
# 1

maximlt avatar Mar 16 '23 11:03 maximlt

Interesting question. Param does behave like standard class attributes when the Parameter is not defined on the subclass:

import param

class A(param.Parameterized):
    p = param.Parameter(1)
    
class B(A):
    pass

print(B.p)
# 1

A.p = 2

print(B.p)
# 2

But when a Parameter is defined on the subclass, the new definition takes precedence, and default value inheritance happens only at instantiation time, not runtime.

We can certainly clarify this in the docs with an explicit statement that default value inheritance only occurs when a subclass does not redefine the parameter. But I'm open to having the subclass dynamically inherit the current default value from the superclass once we have sentinel support to record whether the default value has ever been set in the subclass.

jbednar avatar Mar 23 '23 13:03 jbednar