1. <i id='tS8Qo'><tr id='tS8Qo'><dt id='tS8Qo'><q id='tS8Qo'><span id='tS8Qo'><b id='tS8Qo'><form id='tS8Qo'><ins id='tS8Qo'></ins><ul id='tS8Qo'></ul><sub id='tS8Qo'></sub></form><legend id='tS8Qo'></legend><bdo id='tS8Qo'><pre id='tS8Qo'><center id='tS8Qo'></center></pre></bdo></b><th id='tS8Qo'></th></span></q></dt></tr></i><div id='tS8Qo'><tfoot id='tS8Qo'></tfoot><dl id='tS8Qo'><fieldset id='tS8Qo'></fieldset></dl></div>
    <tfoot id='tS8Qo'></tfoot>
    <legend id='tS8Qo'><style id='tS8Qo'><dir id='tS8Qo'><q id='tS8Qo'></q></dir></style></legend>

      <small id='tS8Qo'></small><noframes id='tS8Qo'>

      • <bdo id='tS8Qo'></bdo><ul id='tS8Qo'></ul>

      返回自我python的目的

      时间:2023-09-02

        <tbody id='8epVF'></tbody>

      <small id='8epVF'></small><noframes id='8epVF'>

        <bdo id='8epVF'></bdo><ul id='8epVF'></ul>

          <tfoot id='8epVF'></tfoot>
        1. <i id='8epVF'><tr id='8epVF'><dt id='8epVF'><q id='8epVF'><span id='8epVF'><b id='8epVF'><form id='8epVF'><ins id='8epVF'></ins><ul id='8epVF'></ul><sub id='8epVF'></sub></form><legend id='8epVF'></legend><bdo id='8epVF'><pre id='8epVF'><center id='8epVF'></center></pre></bdo></b><th id='8epVF'></th></span></q></dt></tr></i><div id='8epVF'><tfoot id='8epVF'></tfoot><dl id='8epVF'><fieldset id='8epVF'></fieldset></dl></div>
        2. <legend id='8epVF'><style id='8epVF'><dir id='8epVF'><q id='8epVF'></q></dir></style></legend>
                本文介绍了返回自我python的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                return self

                class Fib: 
                    def __init__(self, max):
                        self.max = max
                    def __iter__(self): 
                        self.a = 0
                        self.b = 1
                        return self
                    def __next__(self):
                        fib = self.a
                        if fib > self.max:
                            raise StopIteration
                        self.a, self.b = self.b, self.a + self.b
                        return fib
                

                我已经看到了这个问题 return self 问题 但我不明白 return self 有什么好处?

                I have already seen this question return self problem but I can't understand what the benefit is of return self?

                推荐答案

                从一个方法返回 self 仅仅意味着你的方法返回一个对它被调用的实例对象的引用.有时可以在使用面向对象的 API 时看到这一点,这些 API 被设计为 流利的界面,鼓励 方法级联.所以,例如,

                Returning self from a method simply means that your method returns a reference to the instance object on which it was called. This can sometimes be seen in use with object oriented APIs that are designed as a fluent interface that encourages method cascading. So, for example,

                >>> class Counter(object):
                ...     def __init__(self, start=1):
                ...         self.val = start
                ...     def increment(self):
                ...         self.val += 1
                ...         return self
                ...     def decrement(self):
                ...         self.val -= 1
                ...         return self
                ...
                >>> c = Counter()
                

                现在我们可以使用方法级联了:

                Now we can use method cascading:

                >>> c.increment().increment().decrement()
                <__main__.Counter object at 0x1020c1390>
                

                注意,最后一次调用 decrement() 返回 <__main__.Counter object at 0x1020c1390> self.现在:

                Notice, the last call to decrement() returned <__main__.Counter object at 0x1020c1390>, which is self. Now:

                >>> c.val
                2
                >>>
                

                注意,如果你没有返回 self,你不能这样做:

                Notice, you cannot do this if you did not return self:

                >>> class Counter(object):
                ...     def __init__(self, start=1):
                ...         self.val = start
                ...     def increment(self):
                ...         self.val += 1
                ...         # implicitely return `None`
                ...     def decrement(self):
                ...         self.val -= 1
                ...         # implicitely return `None`
                ...
                >>> c = Counter()
                >>> c.increment().increment()
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                AttributeError: 'NoneType' object has no attribute 'increment'
                >>> c
                <__main__.Counter object at 0x1020c15f8>
                >>> c.val
                2
                >>>
                

                请注意,并不是每个人都喜欢方法级联"设计.Python 内置函数不倾向于这样做,因此,list 例如:

                Notice, not everyone is a fan of "method cascading" design. Python built-ins do not tend do this, so, list for example:

                >>> x = list()
                >>> x.append(1).append(2)
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                AttributeError: 'NoneType' object has no attribute 'append'
                >>>
                

                经常看到的一个地方是当你的类实现 iterator 协议时,迭代器上的 iter 返回 self 按照惯例,尽管 文档 建议这样做:

                The one place you do often see this is when your class implements the iterator protocol, where iter on an iterator returns self by convention, although this is suggested by the docs:

                了解了迭代器协议背后的机制后,很容易理解将迭代器行为添加到您的类.定义一个 __iter__() 方法它返回一个带有 __next__() 方法的对象.如果上课定义 __next__(),然后 __iter__() 可以只返回 self:

                Having seen the mechanics behind the iterator protocol, it is easy to add iterator behavior to your classes. Define an __iter__() method which returns an object with a __next__() method. If the class defines __next__(), then __iter__() can just return self:

                class Reverse:
                    """Iterator for looping over a sequence backwards."""
                    def __init__(self, data):
                        self.data = data
                        self.index = len(data)
                
                    def __iter__(self):
                        return self
                
                    def __next__(self):
                        if self.index == 0:
                            raise StopIteration
                        self.index = self.index - 1
                        return self.data[self.index]
                

                注意,这实际上使您的迭代器仅对单次传递有用(因为它应该正确遵循迭代器协议):

                Notice, this in effect makes your iterator only useful for a single pass (as it should be to properly follow the iterator protocol):

                >>> x = [1, 2, 3, 4]
                >>> it = iter(x)
                >>> list(it)
                [1, 2, 3, 4]
                >>> list(it)
                []
                >>> next(it)
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                StopIteration
                >>>
                

                这篇关于返回自我python的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                <i id='RYMGm'><tr id='RYMGm'><dt id='RYMGm'><q id='RYMGm'><span id='RYMGm'><b id='RYMGm'><form id='RYMGm'><ins id='RYMGm'></ins><ul id='RYMGm'></ul><sub id='RYMGm'></sub></form><legend id='RYMGm'></legend><bdo id='RYMGm'><pre id='RYMGm'><center id='RYMGm'></center></pre></bdo></b><th id='RYMGm'></th></span></q></dt></tr></i><div id='RYMGm'><tfoot id='RYMGm'></tfoot><dl id='RYMGm'><fieldset id='RYMGm'></fieldset></dl></div>
                  <tbody id='RYMGm'></tbody>

                <small id='RYMGm'></small><noframes id='RYMGm'>

                <tfoot id='RYMGm'></tfoot>
                  • <bdo id='RYMGm'></bdo><ul id='RYMGm'></ul>
                      <legend id='RYMGm'><style id='RYMGm'><dir id='RYMGm'><q id='RYMGm'></q></dir></style></legend>