0

作成のためにsqlalchemyモデルに渡すことができる他の場所で動作する中間オブジェクトを作成しようとしています:

皮切りに:

class IntermediateObj(object):

    def __init__(self, raw):
        self.raw = raw
        self.sections = []
        self.fields = []
        self.elements = []
        super(IntermediateObj, self).__init__()

    def recurse(self, items):
        for k,v in items.iteritems():
            if isinstance(v, list):
                getattr(self, k).append(v)
                [self.recurse(i) for i in v]
            else:
                setattr(self, k, v)

に渡します:

class MyClass(IntermediateObj, Base):

    def __init__:(self, attribute=None):
         self.attribute = attribute 
         super(MyClass, self).__init__

例えば

ii = IntermediateObj(raw={'large':'nested_dictionary', sections=[{}, {}, {}]})
ii.recurse(ii.raw) #any help smoothing this bit over appreciated as well, but another question...
tada = MyClass(ii)

tada.sections
---> [] /// this should not be, it should correspond to ii.sections

あるべきではないセクションは空なので、ここではまだ継承を完全には把握していません。これはよくある質問ですが、現時点で理解できることは何も見つかりませんでした。さまざまな戦術を駆使しているだけです。Pythonクラスの継承を正しく行うことについての入力はありがたいです。

4

1 に答える 1

0
class IntermediateObj(object):

    def __init__(self, raw):
        self.raw = raw
        self.sections = []
        self.fields = []
        self.elements = []

    def recurse(self, items):
        # this works ok, but passing the same arguments to the recurse function
        # which were passed to the constructor as well ,and then stored as a class
        # attribute, why, seems like self.raw is not even needed?
        for k,v in items.iteritems():
            if isinstance(v, list):
                getattr(self, k).append(v)
                [self.recurse(i) for i in v]
            else:
                setattr(self, k, v)


class MyClass(IntermediateObj):

    def __init__(self, attribute=None):  
        self.attribute = attribute
        super(MyClass, self).__init__(attribute)


ii = IntermediateObj({'large': 'nested_dictionary',
                      'sections': [{}, {}, {}]
                    })
ii.recurse(ii.raw)
print ii.raw
print ii.sections

# passing an instance of IntermediateObj is not the same as passing a dict (logically)
# yet the constructor of MyClass just forwards that instance object to the
# baseclasses constructor, while you initially passed a dict to the IntermediateObj
# constructor. 
tada = MyClass(ii)
# MyClass inherits the recurse method, but it won't magically be executed unless
# you call it, so just instantiating MyClass won't copy those values recursively
tada.recurse(ii.raw)
# now tada, it copied everything, and notice, I called recurse with ii.raw, which is the
# source dictionary, but I'm not even sure if you wanted it that way, it's not clear
# define your question more precisely
print tada.sections

これがベストアンサーとして受け入れられているようですが、実際には回答されていないため、気分が悪くなったので、コメントで説明したように、MyClassのインスタンスがIntermediateObjのインスタンスを受け取ると更新しました。渡されたオブジェクトのrawパラメーターを使用して基本クラスのコンストラクターを呼び出します。また、intermediateObjのコンストラクターでrecurseが呼び出されるため、インスタンス化時に値がコピーされます。

class IntermediateObj(object):

    def __init__(self, raw):
        self.raw = raw
        self.sections = []
        self.fields = []
        self.elements = []
        self.recurse(raw)

    def recurse(self, items):
        for k,v in items.iteritems():
            if isinstance(v, list):
                getattr(self, k).append(v)
                [self.recurse(i) for i in v]
            else:
                setattr(self, k, v)


class MyClass(IntermediateObj):

    def __init__(self, intermediate_instance):  
        super(MyClass, self).__init__(intermediate_instance.raw)


ii = IntermediateObj({'large': 'nested_dictionary',
                      'sections': [{}, {}, {}]
                    })
print ii.raw
print ii.sections
tada = MyClass(ii)
print tada.sections
print tada.raw
于 2012-09-22T17:22:45.860 に答える