如何访问继承 class 的关键字参数默认值
How to access keyword argument default values of inherited class
我正在尝试对 seaborn.JointGrid
class. My plan was to make a child class and inherit most methods from the JointGrid
class 进行一些修改,例如:
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
class 中没有用 __init__
方法中通常的 self.size = size
初始化。也许这就是为什么我无法从我的 child class 访问它们?
如何访问这些变量 size
、ratio
、space
等?
你为什么不使用与你想要子class的class相同的参数?
import seaborn
class CustomJointGrid(seaborn.JointGrid):
def __init__(self, x, y, data=None, size=6, ratio=5, space=.2,
dropna=True, xlim=None, ylim=None, **kwargs):
super().__init__(x, y, data=data, size=size, ratio=ratio, space=space,
dropna=dropna, xlim=xlim, ylim=ylim)
否则你可以自己设置一些默认值,
class CustomJointGrid(seaborn.JointGrid):
def __init__(self, *args, **kwargs):
size = kwargs.get("size", 6)
kwargs.update(size=size)
super().__init__(*args, **kwargs)
# use size here
self.someattribute = size*100
您可以使用 inspect.getfullargspec 来执行此操作:
>>> import seaborn, inspect
>>> spec = inspect.getfullargspec(seaborn.JointGrid.__init__)
>>> defaults = spec.kwonlydefaults or {}
>>> defaults.update(zip(spec.args[-len(spec.defaults):], spec.defaults))
>>> defaults
{'data': None, 'size': 6, 'ratio': 5, 'space': 0.2, 'dropna': True, 'xlim': None, 'ylim': None}
请注意,您的代码只需要执行此操作一次,因为导入的class 的签名不会更改。
我正在尝试对 seaborn.JointGrid
class. My plan was to make a child class and inherit most methods from the JointGrid
class 进行一些修改,例如:
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
class 中没有用 __init__
方法中通常的 self.size = size
初始化。也许这就是为什么我无法从我的 child class 访问它们?
如何访问这些变量 size
、ratio
、space
等?
你为什么不使用与你想要子class的class相同的参数?
import seaborn
class CustomJointGrid(seaborn.JointGrid):
def __init__(self, x, y, data=None, size=6, ratio=5, space=.2,
dropna=True, xlim=None, ylim=None, **kwargs):
super().__init__(x, y, data=data, size=size, ratio=ratio, space=space,
dropna=dropna, xlim=xlim, ylim=ylim)
否则你可以自己设置一些默认值,
class CustomJointGrid(seaborn.JointGrid):
def __init__(self, *args, **kwargs):
size = kwargs.get("size", 6)
kwargs.update(size=size)
super().__init__(*args, **kwargs)
# use size here
self.someattribute = size*100
您可以使用 inspect.getfullargspec 来执行此操作:
>>> import seaborn, inspect
>>> spec = inspect.getfullargspec(seaborn.JointGrid.__init__)
>>> defaults = spec.kwonlydefaults or {}
>>> defaults.update(zip(spec.args[-len(spec.defaults):], spec.defaults))
>>> defaults
{'data': None, 'size': 6, 'ratio': 5, 'space': 0.2, 'dropna': True, 'xlim': None, 'ylim': None}
请注意,您的代码只需要执行此操作一次,因为导入的class 的签名不会更改。