2017-12-26 8 views
0

Aにはold_name()メソッドの基本クラスがありました。最初A.old_name()を呼び出すことにより、Aから継承し、old_name()を上書きするために使用サブクラス:Python:下位互換性を維持しながらメソッドの名前を変更

class A():                 
    '''Old A class'''              

    def old_name(self):              
     print('foo')              

class B(A):                 
    '''Old Subclass.'''              

    def old_name(self):              
     A.old_name(self)             
     print('bar')              

b = B()                  
b.old_name() # must print 'foo bar', prints 'foo bar' 

今は、私がnew_nameold_nameを変更したいです。しかし、私はAPIを壊すことはしたくないので、Bのような古いサブクラスのは、new_name()old_name()の両方の呼び出しをサポートします。また、両方のメソッドをサポートする新しいサブクラス(Cなど)も必要です。これによって、new_name()が呼び出されたか、old_name()が呼び出されたかを問わず、foo barを印刷する必要があります。私はクラスBを変更することができません:変更できる唯一のクラスはクラスAです。

これは私が思いついたものです。残念ながらBはへの直接呼び出しをサポートしていないため、b.old_name()をバイパスします。

class A():                 
    '''New A class. Now A has to support new_name AND old_name.'''   

    def old_name(self):              
     self.new_name()              

    def new_name(self):              
     print('foo')              

class B(A):                 
    '''Old Subclass, still using old_name'''        

    def old_name(self):              
     A.old_name(self)             
     print('bar')              

class C(A):                 
    '''New Subclass, now using new_name'''         

    def new_name(self):              
     A.new_name(self)              
     print('bar')               


b = B()                  
b.new_name() # must print 'foo bar', only prints 'foo'. Bad.      
b.old_name() # must print 'foo bar', prints 'foo bar'. OK.      

c = C()                  
c.new_name() # must print 'foo bar', prints 'foo bar'. OK.      
c.old_name() # must print 'foo bar', prints 'foo bar'. OK. 

どのようなヘルプが大幅にappriciatedでしょう!

+0

'B'のようなクラスBであなたのnew_nameold_nameを上書きする必要があります'new_name'を実装します。また 'warnings'を使って' old_name'が非難されたことを示すべきです。 – jonrsharpe

+0

クラス「B」を変更できないことを明確にするために質問を編集しました。私は間違いなく、この問題がソートされているときに警告を使用します、ありがとう。 – Niourf

+0

次に、new_nameがold_nameを指しているようにコールを取り消さない限り、修正することはできません – jonrsharpe

答えて

0

あなたはb.new_name()からfoo barを印刷するためには、この

class A(object):                 
    '''New A class. Now A has to support new_name AND old_name.'''   

    def old_name(self):              
     self.new_name()              

    def new_name(self):              
     print('foo')              


class B(A):                 
    '''Old Subclass, still using old_name'''        

    def old_name(self):              
     # A.old_name(self)             
     super(B, self).old_name()             
     print('bar')              


class C(A):                 
    '''New Subclass, now using new_name'''         

    def new_name(self):              
     super(C, self).new_name()              
     print('bar')               


b = B()                  

b.new_name() # must print 'foo bar', only prints 'foo'. Bad.      

b.old_name() # must print 'foo bar', prints 'foo bar'. OK.      

c = C()                  

c.new_name() # must print 'foo bar', prints 'foo bar'. OK.      

c.old_name() # must print 'foo bar', prints 'foo bar'. OK. 

が好きでなければならない、あなただけの必要があります。この

class B(A):                 
    '''Old Subclass, still using old_name'''        

    def new_name(self):              
     # A.new_name(self)             
     super(B, self).new_name()             
     print('bar') 

    def old_name(self):              
     # A.old_name(self)             
     super(B, self).old_name()             
     # print('bar')              
+0

クラス「B」は変更できないことを明確にするために質問を編集しました。 – Niourf

+0

なぜ '' 'class B'''を変更できないのですか? –

+0

これはAPIの一部であり、多くのユーザーが既に独自のクラス「B」を実装しているためです。 – Niourf

関連する問題