大类的继承比仅仅调用类中的特定函数需要更多的内存吗?

Chr*_*ris 0 python methods inheritance class function

大类的继承比仅仅调用类中的特定函数需要更多的内存吗?

例如,请参阅下面的A类.哪种方法最快,内存最少?它们是否相同,只是语法问题还是实际上有所作为?

class A(object):
    def function1(self):
        code
    def function2(self):
        code
    def function3(self):
        code
    def function4(self):
        code
    def function5(self):
        code
Run Code Online (Sandbox Code Playgroud)

方法1:

 class B(A):
    def function6(self):
         self.function1(argument)
Run Code Online (Sandbox Code Playgroud)

方法2:

 class B(object):
    def function6(self):
         A().function1(argument)     
Run Code Online (Sandbox Code Playgroud)

方法3:

 class B(object):
    def function6(self):
         A.function1(self,argument) 
Run Code Online (Sandbox Code Playgroud)

在偏好和风格方面,我想采用方法1,但我担心如果A类真的很大并且有很多与B类无关的功能,它将需要更多的内存而不是必需的事情发生了.是这种情况还是我可以使用方法1?

use*_*ica 5

这些选项都没有比其他选项占用更多的内存.但是,它们在同样的情况下也没有意义.

  • 如果你可以打电话A().function1(argument)并做一些合理的事情,function1根本不需要成为一个班级的方法.
  • A.function1(self, argument)如果self不是一个实例,那就没有任何意义A.如果它一个实例A,最好使用self.function1(argument).特别是在Python 2中,A.function1(self, argument)会引发一个TypeErrorif self不是的实例A.
  • self.function1(argument)是最合理的,但如果你继承A只是为了访问它的实例方法,你可能想重新考虑你的程序的类和方法的组织方式.