如何访问 __init__ 中的 *args 变量名称
How to access *args variable name(s) in __init__
如何在 __init__
方法中访问 *args 变量名?
例如
class Parent:
def __init__(self, *args):
# How to access *args variable names here?
# In this example: foo, bar
pass
class Child(Parent):
def __init__(self, foo, bar):
super().__init__(foo, bar)
受 OP 评论的启发,也许你可以做类似的事情
class Parent:
def __init__(self, *args):
self.child_varnames = type(self).__init__.__code__.co_varnames[1:]
class Child(Parent):
def __init__(self, foo, bar):
super().__init__(foo, bar)
在我的测试中,我得到
>>> c = Child(1, 2)
>>> c.child_varnames
('foo', 'bar')
如何在 __init__
方法中访问 *args 变量名?
例如
class Parent:
def __init__(self, *args):
# How to access *args variable names here?
# In this example: foo, bar
pass
class Child(Parent):
def __init__(self, foo, bar):
super().__init__(foo, bar)
受 OP 评论的启发,也许你可以做类似的事情
class Parent:
def __init__(self, *args):
self.child_varnames = type(self).__init__.__code__.co_varnames[1:]
class Child(Parent):
def __init__(self, foo, bar):
super().__init__(foo, bar)
在我的测试中,我得到
>>> c = Child(1, 2)
>>> c.child_varnames
('foo', 'bar')