【发布时间】:2018-10-16 15:41:11
【问题描述】:
我正在尝试对seaborn.JointGrid 类进行一些修改。我的计划是创建一个子类并从JointGrid 类继承大多数方法,如下所示:
import seaborn
class CustomJointGrid(seaborn.JointGrid):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
如果我这样做,我将无法访问变量size、ratio、space 等,它们是__init__ method of JointGrid 的一部分:
def __init__(self, x, y, data=None, size=6, ratio=5, space=.2,
dropna=True, xlim=None, ylim=None)
我注意到这些变量没有在JointGrid 类中使用__init__ 方法中的通常self.size = size 进行初始化。也许这就是为什么我无法从我的子班访问它们的原因?
如何访问这些变量size、ratio、space 等?
【问题讨论】:
-
这些值在
args和kwargs中。见:stackoverflow.com/questions/3394835/args-and-kwargs -
@StephenRauch 这是真的吗?当我执行
print(kwargs.items())和print(*args)时,它只显示我调用CustomJointGrid的参数,而不是我没有传递的“默认”参数。所以如果我说CustomJointGrid("x data", "y data", data=data),变量size是不可用的。 -
默认参数在您继承的源中。不确定我是否理解症结所在。
-
@StephenRauch 但是我如何访问它们?例如,如果我想在我的自定义子类中将
f = plt.figure(figsize=(size, size))更改为类似的内容:f = plt.figure(figsize=(size, 2 * size))它会显示AttributeError: 'CustomJointGrid' object has no attribute 'size' -
@StephenRauch 不确定我是否理解。您的意思是将
size添加到super().init(size=5, *args, **kwargs)?
标签: python arguments subclass seaborn default-value