多くの場合、クラスのコンストラクターはその引数を取り、インスタンスに保存します。例えば:
class Example(object):
def __init__(self, title='',backtitle='', height=20, width=50):
self.title = title
self.backtitle = backtitle
self.height = height
self.width = width
これは繰り返しなので、これを自動的に行うヘルパー関数を作成しました。
from inspect import getargspec
def save_args(values):
for i in getargspec(values['self'].__init__).args[1:]:
values['self'].__dict__[i] = values[i]
class Example(object):
def __init__(self, title='',backtitle='', height=20, width=50):
save_args(vars())
私の質問は次のとおりです。
- これは特定のクラスまたは引数で失敗しますか
- 移植可能ですか、Jythonなどで動作しますか..Python 2.7および3.2で動作しました
- より簡単な代替手段はありますか?
- すでにこれを行っているpythonパッケージはありますか?