简体   繁体   English

通过将函数传递给Python中的类构造函数来轻松更改方法

[英]Slighly alter method by passing function to class constructor in Python

I'm writing a class which needs to use slightly different code depending on the instance. 我正在写一个需要根据实例使用略有不同的代码的类。

I could achieve this through inheritance, but I wondered whether it would would be simpler to do this by passing a function to constructor when an instance is instantiated. 我可以通过继承来实现,但是我想知道在实例化实例时将一个函数传递给构造函数是否会更简单。

I'm thinking something like this: 我在想这样的事情:

class myClass():

    def __init__(self, fn):
        self.myProperty = 1
        self.instanceSpecificFunction = fn

    def myMethod(self):

        #Do things here that are common to all instances of class

        #[common code would be here]

        #But then run some code that is specific to this instance of the class
        self.instanceSpecificFunction(self)

def functionThatsSpecificToThisInstance(self):

    #In general, this code would want 
    #to be different if we instantiated a different instance

    self.myProperty = 3

myInstance = myClass(functionThatsSpecificToThisInstance)  

myInstance.myMethod()

Is this a terrible idea? 这是一个可怕的主意吗? Does this kind of design pattern have a name? 这种设计模式有名称吗? If so is there a 'best' way to this? 如果是这样,是否有“最佳”方法?

Thanks very much, 非常感谢,

Robin 知更鸟

Actually, I don't like the idea much. 实际上,我不太喜欢这个主意。

First of all, you'll have random functions not belonging to any class in particular but that actually use self as arguments (although self is just a normal function argument, that's something in the code that doesn't smell very good ). 首先,您将拥有不属于任何类的随机函数,但它们实际上使用self作为参数(尽管self只是一个普通的函数参数,这在代码中有些难闻 )。

Having functions wandering around in a module usually makes me uncomfortable, unless the module is just a helper module filled only with helper functions . 使功能在模块中徘徊通常会让我感到不舒服,除非该模块仅仅是仅填充了辅助功能辅助模块 But mixing classes and functions and these functions actually performing some work on classes' instances is kind of messy from my point of view. 但是,从我的角度来看,将类和函数以及这些函数实际在类的实例上执行某些工作混合在一起有点混乱。

Inheritance looks great here. 继承在这里看起来很棒。 Just redefine a different constructor for every class that needs it and, when other developers see the code they will have a better idea of what's going on. 只需为每个需要它的类重新定义一个不同的构造函数,当其他开发人员看到代码时,他们将对发生的事情有了更好的了解。 At the end of the day you need to code the logic for those functions, so why not just code it inside the classes their instances represent . 最终,您需要为这些函数编写逻辑代码,所以为什么不直接在其实例表示的类中进行编码。

Also, you may want to take a look to the Factory Pattern . 另外,您可能想看看Factory Pattern It is a creational pattern that might fill your needs. 这是一种创新的模式 ,可能会满足您的需求。 The idea you have makes sense, but the implementation is rather messy. 您的想法很有道理,但是实现起来很混乱。 By following a pattern you make sure to take the rights step to create nice code, understandable and reusable. 通过遵循一种模式,您确保采取权限步骤来创建可理解且可重用的漂亮代码。 Take also a look at several creational patterns , maybe one of them is what you're looking for. 还要看看几种创作模式 ,也许其中一种就是您要寻找的。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM