• <small id='VLshM'></small><noframes id='VLshM'>

      <bdo id='VLshM'></bdo><ul id='VLshM'></ul>

    1. <legend id='VLshM'><style id='VLshM'><dir id='VLshM'><q id='VLshM'></q></dir></style></legend>

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

        如何将自定义类对象转换为 Python 中的元组?

        How to convert an custom class object to a tuple in Python?(如何将自定义类对象转换为 Python 中的元组?)
              <tbody id='M27L8'></tbody>

            • <small id='M27L8'></small><noframes id='M27L8'>

                <legend id='M27L8'><style id='M27L8'><dir id='M27L8'><q id='M27L8'></q></dir></style></legend>
                  <bdo id='M27L8'></bdo><ul id='M27L8'></ul>
                • <i id='M27L8'><tr id='M27L8'><dt id='M27L8'><q id='M27L8'><span id='M27L8'><b id='M27L8'><form id='M27L8'><ins id='M27L8'></ins><ul id='M27L8'></ul><sub id='M27L8'></sub></form><legend id='M27L8'></legend><bdo id='M27L8'><pre id='M27L8'><center id='M27L8'></center></pre></bdo></b><th id='M27L8'></th></span></q></dt></tr></i><div id='M27L8'><tfoot id='M27L8'></tfoot><dl id='M27L8'><fieldset id='M27L8'></fieldset></dl></div>
                  <tfoot id='M27L8'></tfoot>
                  本文介绍了如何将自定义类对象转换为 Python 中的元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我们在一个类中定义__str__方法:

                  If we define __str__ method in a class:

                      class Point():
                          def __init__(self, x, y):
                              self.x = x
                              self.y = y
                  
                  
                          def __str__(self, key):
                              return '{}, {}'.format(self.x, self.y)
                  

                  我们将能够定义如何将对象转换为 str 类(转换为字符串):

                  We will be able to define how to convert the object to the str class (into a string):

                      a = Point(1, 1)
                      b = str(a)
                      print(b)
                  

                  我知道我们可以定义自定义对象的字符串表示,但是我们如何定义对象的列表——更准确地说,元组——表示?

                  I know that we can define the string representation of a custom-defined object, but how do we define the list —more precisely, tuple— representation of an object?

                  推荐答案

                  tuple 函数"(它实际上是一个类型,但这意味着你可以像函数一样调用它)将接受任何可迭代的,包括一个迭代器,作为它的参数.因此,如果要将对象转换为元组,只需确保它是可迭代的.这意味着实现一个 __iter__ 方法,该方法应该是一个生成器函数(其主体包含一个或多个 yield 表达式).例如

                  The tuple "function" (it's really a type, but that means you can call it like a function) will take any iterable, including an iterator, as its argument. So if you want to convert your object to a tuple, just make sure it's iterable. This means implementing an __iter__ method, which should be a generator function (one whose body contains one or more yield expressions). e.g.

                  >>> class SquaresTo:
                  ...     def __init__(self, n):
                  ...         self.n = n
                  ...     def __iter__(self):
                  ...         for i in range(self.n):
                  ...             yield i * i
                  ...
                  >>> s = SquaresTo(5)
                  >>> tuple(s)
                  (0, 1, 4, 9, 16)
                  >>> list(s)
                  [0, 1, 4, 9, 16]
                  >>> sum(s)
                  30
                  

                  您可以从示例中看到,几个 Python 函数/类型将采用可迭代对象作为其参数,并使用它生成的值序列来生成结果.

                  You can see from the example that several Python functions/types will take an iterable as their argument and use the sequence of values that it generates in producing a result.

                  这篇关于如何将自定义类对象转换为 Python 中的元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <legend id='fGVzA'><style id='fGVzA'><dir id='fGVzA'><q id='fGVzA'></q></dir></style></legend>

                      <tbody id='fGVzA'></tbody>

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

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