在Python中的对象上使用类外构建的函数?
我有一节课在Python中的对象上使用类外构建的函数?,python,function,class,object,methods,Python,Function,Class,Object,Methods,我有一节课 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
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`
它创建一个列表
对象。我有一个功能(不确定它是否工作)
def排序(列表):
排序=假
i=0
而i
我想在列表
对象上运行此函数,而不向类中添加其他方法。我知道如果这是在类中,那么它就是List.sorted(),但是如果不是objects方法,我如何在对象上运行这个函数呢
sorted(List)
似乎也不起作用。请帮助。请,请,请:不要将排序的
用作函数名。已经有一个标准(内置)函数sorted()
,它返回iterable的排序版本
考虑到您的代码只是检查列表是否有序,也许您可以将其称为“按顺序”或“按顺序”或“按升序”
也就是说,让我们尝试让您的代码正常工作:
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())
类节点:
定义初始化(self,data=None,next=None):
self.data=数据
self.next=下一个
定义(自我):
返回str(self.data)
班级名单:
定义初始化(self,head:Node=None,tail:Node=None):
self.head=如果head不是其他head,则为tail
self.tail=如果tail不是其他tail,则为head
def cons(自身,节点:节点):
node.next=self.head
返回列表(节点,self.tail)
定义为空(self):
返回self.head==无
def显示(自):
返回str(self)
定义(自我):
结果=“[”
第一个=正确
n=自个头
虽然n不是无:
如果不是首先:
结果+=“,”
结果+=str(n)
如果n==self.tail:
打破
下一个
第一个=错误
结果+=“]”
返回结果
def ascendingp(自)->bool:
n=自个头
最后一个=无
最后一次!=self.tail:
如果last不是None且不是last.data
在对象上运行此函数是什么意思?如果您想使用self
,只需使用list
。是的,我认为我没有正确解释这一点,但基本上list类创建了一个名为list的对象。我想通过在列表上运行排序函数来确定该列表是否已排序。但是,当我通过执行排序(list)调用它时,我得到错误“list”对象不是iterable您将如何创建一个包含三项的list
?是的。考虑到问题被标记为“python”,并且python2已经过时8年了,这似乎是合理的,你不觉得吗?OP没有使用python-3标记。Python2.7将再支持四年。您至少应该非常清楚,您的代码只能在Python-3上工作。
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())