Python类 - 链接实例方法

时间:2017-01-03 19:05:26

标签: python class instantiation

我已经开始以这种方式构建我的class(更像是常规function):

ex 1

  class Child(Parent):

    def __init__(self,user):
        Parent.__init__(self, user)     

        search = api.search() 
        filter = api.filter(search)
        output = api.output(filter)

        return output

这样我可以跑:

movies = Child('John')

并获得我的最终输出。

然后,我将global api methods分成了instance methods,以便更好地控制数据:

ex 2

  class Child(Parent):

    def __init__(self,user):
        Parent.__init__(self, user)     

    def search_api(self):
       search = api.search() 
       return search    

    def filter_api(self, search):
       filter = api.filter(search)
       return filter

    def output(self, filter):
       output = api.output(filter)  
       return output

现在我必须在不同的实例中打破它,以获得最终输出:

test = Child('John')
search = test.search_api()
filter = test.filter_api(search)
out = test.output(filter)

print (out)

是否有decoratorbuilt-in method允许我链接所有instance methods以便{1}}可以一次性运行(实例化)?

4 个答案:

答案 0 :(得分:2)

我认为您所追求的是构建器模式,因此您可以编写output = Search().filter().get_output()或类似内容。

为此,您需要从所有“中间”方法返回构建器对象:

class Search(object):
    def __init__(self, *args, **kwargs):
        # apply args, kwargs

    def filter(self, *args, **kwargs):
        # apply args, kwargs
        self.filters.append(...)
        return self

    def paginate(self, *args, **kwargs):
        # apply args, kwargs
        self.pagination = ...
        return self

    def get_output(self, *args, **kwargs):
        # apply args, kwargs
        # build the search, find output ...
        return output

答案 1 :(得分:1)

如果您需要使对象的行为类似函数调用,则应该实现__call__而不是仅仅在构造函数中打包所有内容。

class Child(Parent):

    def __init__(self,user):
        Parent.__init__(self, user)
        self.myuser = user


    def search_api(self):
       search = api.search() 
       return search    

    def filter_api(self, search):
       filter = api.filter()
       return filter

    def output(self, filter): 
       return output

    def __call__(self,user):
        search = self.search_api()
        filter = self.filter_api(search)
        return self.output(filter)

现在你可以:

a = Child('Joe')
print(a())

答案 2 :(得分:1)

让它编译的一些助手:

class API:
    def __getattr__(self, attr):
        return API()
    def __call__(self, x=1):
        return 42

api = API()

class Parent(object):
     def __init__(self, user):
        self.user = user

执行process方法中的所有步骤:

class Child(Parent):

    def __init__(self, user):
        super().__init__(user)     

    def search_api(self):
        search = api.search() 
        return search    

    def filter_api(self, search):
        filter = api.filter()
        return filter

    def output(self, filter):   
        return api.output

    def process(self):
        res = self.search_api() 
        res = self.filter_api(res)
        return self.output(res)

允许您在一行中完成所有操作:

movies = Child('John').process()

答案 3 :(得分:1)

您可以使用__call__()。我知道它不一样,但也许它可能比你的ex2

更好
class Child(Parent):

    def __init__(self, user):
        Parent.__init__(self, user)

    def search_api(self):
        search = api.search() 
        return search    

    def filter_api(self, search):
        filter = api.filter(search)
        return filter

    def output(self, filter):
        output = api.output(filter)  
        return output

    def __call__(self):
        search = self.search_api()
        filter = self.filter_api(search)
        return self.output(filter)

然后,你可以这样做:

c = Child("john")
output = c()