繁体   English   中英

如何在 Python 中创建适用于类的多值函数?

[英]How do I create a multi-value function that applies to a class in Python?

我创建了一个类并试图创建一个函数,该函数本质上将充当该类的两个对象之间的二元运算符。 我试图创建的函数称为“组合”。

我知道我可以在类之外创建函数,但我希望它与类相关联。

背景(回答问题并不是必需的)- 该课程正在为 S4 数学组建模,并且对我来说是对象和课程的一些练习。 我的下一个功能是将一个元素简化为它最简单的循环表达式,我相信我可以做到,但我宁愿先对这个元素进行排序。

当我用一个参数创建一个函数时,它运行良好 - 如下面的代码所示,使用函数“cycletype”,它按预期工作。

class s4:

    # Define an element of s4, elementlist is a nested list, which is a list of the cycles composing the element.

    def __init__(self, elementlist):
        self.elementlist = elementlist

    # One simple function is to ascertain the cycletype of the element.

    def cycletype(self):
        cycles = []
        for i in self.elementlist:
            cycles.append(len(i))
        return cycles

    # Combining two elements using the group operation is the first function to define.

    def combine(first, second):
        for i in second:
            first.append(i)
        return first

double = s4([[1,2],[3,4]])
triple = s4([[1,2,3]])

print(combine(double,triple))

我期待打印 [[1,2],[3,4],[1,2,3]] ,但是,它显示了 NameError,无法识别组合。

代码有两个问题

  • 将函数放在类中意味着它是一个方法。 因此,您必须使用对象s4.combine(s1)访问它,而不仅仅是combine(...) 否则,它将是一个函数,而不是一个方法。
  • 更改后:您不能for i in second编写,因为您的类的实例不可迭代。 您必须实现__iter__才能使用该语法

这是因为 combine 函数不存在于全局范围内(我希望这就是它的名字,我把名字弄乱了真的很糟糕)。

您只能调用全局作用域中存在的函数,对于类,您需要对象来调用这些函数,因为这些函数是那些特定类的一部分,而不是一般的全局作用域。

希望这有帮助

您应该从由两个参数包装的列表中创建一个实例:

class S4:

    # Define an element of S4, elementlist is a nested list, which is a list of the cycles composing the element.

    def __init__(self, elementlist):
        self.elementlist = elementlist

    # One simple function is to ascertain the cycletype of the element.

    def cycletype(self):
        cycles = []
        for i in self.elementlist:
            cycles.append(len(i))
        return cycles

    # Combining two elements using the group operation is the first function to define.

    def combine(self, first, second):
        return S4(first.element_list + second.element_list)

看来您也可以简单地定义__add__而不是combine

# Simplified implementation, no error checking
def __add__(self, second):
    return S4(self.element_list + second.element_list)

允许你写

print(double + triple)

代替

print(combine(double, triple))

存在范围问题,您在 s4 类中定义了 combine ,因此您应该从 s4 的实例中调用它。 无论如何,这就是我将如何做到的。

class s4:

    # Define an element of s4, elementlist is a nested list, which is a list of the cycles composing the element.
    def __init__(self, elementlist):
        self.elementlist = elementlist

    # One simple function is to ascertain the cycletype of the element.

    def cycletype(self):
        cycles = []
        for i in self.elementlist:
            cycles.append(len(i))
        return cycles

    # Combining two elements using the group operation is the first function to define.

    def combine(self, second):
      #Here I changed first to self, first would be the s4 class that calls the function, in this example double and second would be the one that you want to combine
        first = self.elementlist
        for i in second.elementlist:
            first.append(i)
        return first

double = s4([[1,2],[3,4]])
triple = s4([[1,2,3]])
##As double() is defined within the s4 class, you can only call it from an instance of s4 (in this case you could use doble.combine() or triple.combine())
print(double.combine(triple))

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM