views:

56

answers:

1

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?

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; In the actual code I have another function that checks to see if something1 or something2 is expired; 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.


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).

+4  A: 

Don't use get_* methods. In Python the better way is to use properties:

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.

Once you have working code, you can see how something2 is being used. 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.

unutbu