【问题标题】:Use a function built outside the class on an object in Python?在 Python 中的对象上使用在类之外构建的函数?
【发布时间】:2016-02-18 14:54:01
【问题描述】:

我有课

class List:

    def __init__(self,head,tail):
        self.head = head
        self.tail = tail

    def cons(self,item):
        return List(item,self)

    def isEmpty(self):
        return self.head == None

    def display(self):
        s = "["
        first = True
        list = self
        while not list.isEmpty():
            if not first:
                s=s+","
            first = False
            s=s+str(list.head)
            list = list.tail
        s=s+"]"
        return s`

创建一个List 对象。我有一个功能(不确定它是否有效);

def sorted(list):
    sort = False
    i = 0
    while i < range(len(list))+1:
        if list[i] < list[i+1]:
            sort = True
            return sort 
        else:
            return sort

我想在List 对象上运行此函数,而不向类添加其他方法。我知道这是否在类中,它只是 List.sorted() 但我如何在对象上运行此函数而不是对象方法?

sorted(List) 似乎也不起作用。请帮忙。

【问题讨论】:

  • 你是什么意思在对象上运行这个函数?如果您的意思是,您希望能够使用self,只需使用list。
  • 是的,我认为我没有正确解释这一点,但基本上 List 类创建了一个名为 list 的对象。我想通过在其上运行我的 sorted 函数来确定该列表是否已排序。但是当我通过 sorted(list) 调用它时,我得到错误'List' object is not iterable
  • 你将如何创建一个包含三个项目的List?

标签: python function class object methods


【解决方案1】:

请,请,请:不要使用sorted 作为函数名。已经有一个标准(内置)函数sorted(),它返回一个可迭代的排序版本。

鉴于您的代码只是检查列表是否有序,也许您可​​以将其称为 in_order 或 ordered 或 is_ascending?

也就是说,让我们尝试让您的代码正常工作:

class Node:
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next

    def __str__(self):
        return str(self.data)

class List:
    def __init__(self, head:Node=None, tail:Node=None):
        self.head = tail if head is None else head
        self.tail = head if tail is None else tail

    def cons(self, node:Node):
        node.next = self.head
        return List(node, self.tail)

    def isEmpty(self):
        return self.head == None

    def display(self):
        return str(self)

    def __str__(self):
        result = "["
        first = True

        n = self.head
        while n is not None:
            if not first:
                result += ", "
            result += str(n)
            if n == self.tail:
                break
            n = n.next
            first = False

        result += "]"
        return result

    def ascendingp(self) -> bool:
        n = self.head
        last = None

        while last != self.tail:
            if last is not None and not last.data < n.data:
                return False
            last = n
            n = n.next

        return True

tests = (
    (1,2,3),
    (1,3,2),
    (),
)

for t in tests:
    last = None
    first = None
    for i in reversed(t):
        n = Node(data=i)
        first = n if first is None else first
        n.next = last
        last = n

    l = List(head=last, tail=first)
    print(str(l), "Ascending?", l.ascendingp())

【讨论】:

  • 是的。鉴于问题被标记为“python”,并且 python 2 已经过时 8 年了,这似乎是合理的,你不觉得吗?
  • OP 没有使用 python-3 标签。 Python 2.7 将支持四年多。您至少应该非常清楚您的代码只能在 Python-3 上运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-15
  • 2021-03-18
  • 1970-01-01
  • 2012-11-25
  • 1970-01-01
  • 2012-11-05
  • 2018-06-02
相关资源
最近更新 更多