Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python面向对象设计;返回、设置实例变量或两者_Python_Oop_Object - Fatal编程技术网

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

Python面向对象设计;返回、设置实例变量或两者,python,oop,object,Python,Oop,Object,好的,我有一些代码可以归结为这样的模式 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_someth

好的,我有一些代码可以归结为这样的模式

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"
我的问题是,我应该做上述(方法通过实例变量获得它们所需要的)还是通过这样的参数;我还应该在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"
现在在我的代码中,方法返回延迟(twisted);现在,当延迟触发时,它设置实例变量并返回None;在实际代码中,我有另一个函数,它检查something1或something2是否过期;我更新了它们,这带来了一个问题,因为我像这样更新它

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
所以现在更新代码要么丑陋,要么充满错误,要么两者兼而有之。所以我有一种感觉,我在做一些事情,要么是a)非蟒蛇式的,要么是b)非扭曲式的,要么是c)其他糟糕的设计



谢谢您的回答,但这些都不起作用,因为函数返回的是延迟值,而不是实际值(否则它必须阻塞)。

不要使用
get.*
方法。在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"
请注意,当
something1
是属性时,
self.something1
(不带括号!)调用相应的函数
Foo.something1

一旦你有了工作代码,你就可以看到
something2
是如何被使用的。如果两者都有

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

    @property
    def something3(self):
        return self.otherthing1 + "_something_2"
然后您可能需要重构代码以使用它

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

我们对你的情况了解不够,无法告诉你哪一个更好。但是,一旦您看到工作代码的使用模式,答案可能会变得显而易见。

不要使用
get.*
方法。在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"
请注意,当
something1
是属性时,
self.something1
(不带括号!)调用相应的函数
Foo.something1

一旦你有了工作代码,你就可以看到
something2
是如何被使用的。如果两者都有

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

    @property
    def something3(self):
        return self.otherthing1 + "_something_2"
然后您可能需要重构代码以使用它

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

我们对你的情况了解不够,无法告诉你哪一个更好。但是,一旦您看到工作代码的使用模式,答案可能会变得显而易见。

因为我们不知道您的设计的任何其他部分,所以我们无法回答这样的“应该”问题。如果你需要的话,你应该这样做。因为我们不知道你设计的任何其他部分,我们不能回答像这样的“应该”的问题。如果你需要的话,你应该去做。