如何在__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)
uj5u.com熱心網友回復:
受 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')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369005.html
