-4

我正在为GMAIL API编写一个包装器。在这个包装器中,我试图在“主类”中包含子属性,因此它更接近于以下内容:

图片

以前,我使用的方法如下:

class Foo:
    def __init__(self, ...):
        # add some attributes

    def get_method(self, ...):
        return some_stuff

这让我可以做到foo.get_method(...)。要遵循 GMAIL API,我尝试执行以下操作:

class Foo:
     def __init__(self, ...):
         # add some attributes

     @property
     def method(self):
         class _Method:
             @staticmethod
             def get(self, ...):
                 return some_stuff
         return _Method()

这让我可以做到foo.method.get(...)。上面有一些问题,它每次都重新定义类,我必须在@staticmethod每个方法之上添加作为它的一部分。我确实意识到我可以在外部类级别创建类,并为每个隐藏变量设置一个隐藏变量,然后 .method 返回或创建,但这似乎是太多的解决方法。

tldr:是否可以将传递给内部类self的实例作为外部类的实例(我不希望将外部类的属性传递给每个内部类)。

4

3 回答 3

1

用户应该有一个消息实例,它允许方法获取。代码的草图是:

class Messages:
      ...
      def get()
            ...


class Users:
      ...
      messages = Messages(...)

允许

users =  Users()
users.messages.get()

这个 API 的坏处是复数名称,这对类来说是个坏兆头。如果从头开始,您宁愿拥有 User 和 Message 类,这更有意义。

如果您仔细查看您提供的链接 API 中的 GET/POST 调用,您会注意到 URL 类似于UserId/settings,这是实现 User 类而不是 Users 的另一个提示。

于 2018-09-16T02:05:51.813 回答
1

与其self在类之间共享参数,不如将所需的东西传递给您实例化的类的构造函数。

class Messages:
    def __init__(self, name):
        self.name = name

    def method(self, other_arg):
        return self.name + other_arg

class Test:
    name = "hi"

    def __init__(self):
        self.messages = Messages(name=self.name)

如果您需要将大量信息传递给构造函数并且它开始变得笨拙,您可以执行一些操作,例如将共享代码拆分为第三个类,然后将其作为单个对象在 Test 和 Messages 类之间传递。

在 Python 中,您可以使用元类和魔术方法来做各种聪明的事情,但在 99% 的情况下,只需将事物重构为不同的类和函数,就可以让您的代码更具可读性和可维护性。

于 2018-09-16T02:09:28.863 回答
0

方法中的 self 引用了外部类的 self

也许这就是你想要的工厂方法

尽管我将在下面提供的示例代码可能与已经提供的答案相似,并且上面指向另一个答案的链接可能会满足您的愿望,因为它的形式略有不同,但我仍然会就您的要求提供我的看法。该代码是不言自明的。

class User:
    def __init__(self, pk, name):
        self.pk = pk
        self.name = name
        self._messages = None

    def messages(self):
        if self.messages is None:
            self._messages = Messages(self.pk)
        return self._messages


class Messages:
    def __init__(self, usr):
        self.usr = usr

    def get(self):
        return self._grab_data()

    def _grab_data(self):
        # grab the data from DB
        if self.usr == 1:
            print('All messages of usr 1')
        elif self.usr == 2:
            print('All messages of usr 2')
        elif self.usr == 3:
            print('All messages of usr 3')



one = User(1, 'One')
two = User(2, 'Two')
three = User(3, 'Three')

one.messages().get()
two.messages().get()
three.messages().get()

对于标签、历史等,实用的messages方法方法是相同的。

编辑:我会再试一次,试图理解你想要达到的目标,即使你这么说

我已经尝试了很多方法来定义容器类之外的类 [...]

. 我不知道你是否尝试过继承,因为你的内部类me,尽管它在这里并不代表什么,但看起来你仍然想以某种方式利用它的功能。你也说

方法中的 self 引用了外部类的 self

在我看来,这听起来像是您最终想要继承。那么要走的路是(通过使用继承的接近想法):

class me(object):
    def __init__(self):
        self.__other_arg = None # private and hidden variable
    # setter and getter methods
    def set_other_arg(self, new_other_arg):
        self.__other_arg = new_other_arg
    def get_other_arg(self): 
        return self.__other_arg

class Test(me):
    name = 'Class Test'
    @property
    def message(self):
        other_arg = self.get_other_arg()
        if other_arg is not None:
            return '{} {}'.format(self.name, other_arg)
        else:
            return self.name

t = Test()
t.set_other_arg('said Hello')
print(t.message)
# output >>> Class Test said Hello

我认为这可能是一种更可取的方式,而不是你的内部阶级方法,我认为,你会决定的。只是一个侧面说明,在 python 中查找 getter 和 setter,如果你想坚持给定的继承思想,它可能会对你有所帮助。

于 2018-09-16T03:47:09.673 回答