简体   繁体   English

Python面向对象设计; 返回,设置实例变量或两者

[英]Python Object Oriented Design; Return, Set Instance Variable Or Both

Ok I have some code that boils down to a pattern like this 好吧,我有一些代码可以归结为这样的模式

class Foo(object):
def get_something1(self):
    # in the actual code it asks a web server but here I replaced that with "foo_something1" instead of the code from a web server
    self.something1 = "foo_something1"

def get_something2(self):
    # needs the result of get_something1; right now I have it get it by instance variable like so
    self.something2 = self.something1 + "_something_2"

My question is, should I do the above (methods getting what they need by instance variables) or by taking arguments like so; 我的问题是,我是否应该执行上述操作(方法通过实例变量获取所需的方法)或采用类似的参数; also should I return something1 and something2 in the get_* methods? 我应该在get_ *方法中返回something1和something2吗?

class Foo(object):
def get_something1(self):
    # in the actual code it asks a web server but here I replaced that with "foo_something1" instead of the code from a web server
    self.something1 = "foo_something1"

def get_something2(self, something_1):
    # needs the result of get_something1; right now I have it get it by instance variable like so
    self.something2 = something1 + "_something_2"

Now in my code the methods return deferreds (twisted); 现在在我的代码中,方法返回延迟(扭曲); Right now when the deferreds fire it sets the instance variables and returns None; 现在,当deferreds触发时,它设置实例变量并返回None; In the actual code I have another function that checks to see if something1 or something2 is expired; 在实际的代码中,我有另一个函数来检查something1或something2是否过期; I update them which brings up a problem because I update it like so... 我更新它们会带来一个问题,因为我这样更新它...

d = self.get_something1()
##d.addCallback(self.get_something2) # errors because it gets the argument None (so I do below)
d.addCallback(lambda ignored: self.get_something2())# problem the deferred for get_something2 is inaccessible

So right now the update code is either ugly, error filled, or both. 所以现在更新代码要么是丑陋的,要么是错误的,要么两者兼而有之。 So I have a feeling that I am doing something either a) un pythonic or b) un twisted or c) some other bad design. 所以我有一种感觉,我做的事情要么a)非pythonic或b)un twisted或c)其他一些糟糕的设计。


Thanks for the answers, but those won't work because the functions return deferreds and not the actual value (other wise it would have to block). 谢谢你的答案,但那些不起作用,因为函数返回延迟而不是实际值(否则它必须阻止)。

Don't use get_* methods. 不要使用get_*方法。 In Python the better way is to use properties: 在Python中,更好的方法是使用属性:

class Foo(object):
    @property
    def something1(self):
        # in the actual code it asks a web server but here I replaced that with "foo_something1" instead of the code from a web server
        return "foo_something1"
    @property
    def something2(self):
        # needs the result of get_something1; right now I have it get it by instance variable like so
        return self.something1 + "_something_2"

Note that when something1 is a property, self.something1 (without parentheses!) calls the corresponding function Foo.something1 . 请注意,当something1是属性时, self.something1 (没有括号!)调用相应的函数Foo.something1

Once you have working code, you can see how something2 is being used. 一旦你有了工作代码,就可以看到something2是如何被使用的。 If you have both 如果你有两个

    @property
    def something2(self):
        return self.something1 + "_something_2"

    @property
    def something3(self):
        return self.otherthing1 + "_something_2"

Then you might want to refactor the code to use 然后,您可能希望重构要使用的代码

    def something2(self,prefix):
        return prefix+"_something_2"

We don't know enough about your situation to tell you which is better. 我们对你的情况了解不多,告诉你哪个更好。 But the answer may become obvious to you once you see the use patterns of your working code. 但是,一旦看到工作代码的使用模式,答案就会变得很明显。

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

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