如何在Python中修改参数以干净地运行?
我有以下代码,用于修改递归传递到函数中的节点(将节点包装在数组中,以便在函数返回后修改是持久的): 有没有更好或更干净的方法来修改这个论点 ` `如何在Python中修改参数以干净地运行?,python,function,Python,Function,我有以下代码,用于修改递归传递到函数中的节点(将节点包装在数组中,以便在函数返回后修改是持久的): 有没有更好或更干净的方法来修改这个论点 ` ` 问题已在中得到回答:,因为您正在操作的参数是对象。您可以使用\uuuu dict\uuuu更改对象的整个属性。这相当于改变项目的每个属性。您可以尝试以下代码: class node(object): def __init__(self, value, next=None): self._value = value
问题已在中得到回答:,因为您正在操作的参数是对象。您可以使用
\uuuu dict\uuuu
更改对象的整个属性。这相当于改变项目的每个属性。您可以尝试以下代码:
class node(object):
def __init__(self, value, next=None):
self._value = value
self._next = next
def __repr__(self):
return "node({0})".format(self._value)
@property
def next(self):
return self._next
def foo(a):
print "setting to next node\n",
a.__dict__ = getattr(a.next, '__dict__', None)
return
n = node(5, node(8, node(7)))
print n._value, '->' ,n._next._value
foo(n)
print n._value, '->' ,n._next._value
希望这能对您有所帮助。因为您正在操作的参数是对象。您可以使用
\uuuu dict\uuuu
更改对象的整个属性。这相当于改变项目的每个属性。您可以尝试以下代码:
class node(object):
def __init__(self, value, next=None):
self._value = value
self._next = next
def __repr__(self):
return "node({0})".format(self._value)
@property
def next(self):
return self._next
def foo(a):
print "setting to next node\n",
a.__dict__ = getattr(a.next, '__dict__', None)
return
n = node(5, node(8, node(7)))
print n._value, '->' ,n._next._value
foo(n)
print n._value, '->' ,n._next._value
希望这能对您有所帮助。要修改某些内容,该内容必须是可变的。您的
节点
实例是可变的:
n = node(3)
assert n.value == 3
n.value = 5
assert n.value == 5 # it was modified!
此外,函数无法返回任何值。在你的情况下,这可能是一个错误的方法。而且,坦率地说,我不明白为什么要在引用.next
值的地方使用number(0
,n-1
)。这些必须是节点实例,而不是数字
显然,您正在进行一个链表实现,并且您的foo
函数试图通过遍历列表来删除第n个节点。(请注意以描述性的方式命名您的功能;这有助于您和其他人回答您的问题。)
我就是这样做的:
class Node(object): # class names are usually TitleCase
def __init__(self, value, next=None):
self.value = value
self.next = next # no properties for simplicity
def __repr__(self):
return "node({0})".format(self.value)
def asList(node): # nice for printing
if not node:
return []
return [node.value] + asList(node.next)
def removeNodeAt(head_node, index):
"""Removes a node from a list. Returns the (new) head node of the list."""
if index == 0: # changing head
return head_node.next
i = 1 # we have handled the index == 0 above
scan_node = head_node
while i < index and scan_node.next:
scan_node = scan_node.next
i += 1
# here scan_node.next is the node we want removed, or None
if scan_node.next:
scan_node.next = scan_node.next.next # jump over the removed node
return head_node
要修改某个内容,该内容必须是可变的。您的
节点
实例是可变的:
n = node(3)
assert n.value == 3
n.value = 5
assert n.value == 5 # it was modified!
此外,函数无法返回任何值。在你的情况下,这可能是一个错误的方法。而且,坦率地说,我不明白为什么要在引用.next
值的地方使用number(0
,n-1
)。这些必须是节点实例,而不是数字
显然,您正在进行一个链表实现,并且您的foo
函数试图通过遍历列表来删除第n个节点。(请注意以描述性的方式命名您的功能;这有助于您和其他人回答您的问题。)
我就是这样做的:
class Node(object): # class names are usually TitleCase
def __init__(self, value, next=None):
self.value = value
self.next = next # no properties for simplicity
def __repr__(self):
return "node({0})".format(self.value)
def asList(node): # nice for printing
if not node:
return []
return [node.value] + asList(node.next)
def removeNodeAt(head_node, index):
"""Removes a node from a list. Returns the (new) head node of the list."""
if index == 0: # changing head
return head_node.next
i = 1 # we have handled the index == 0 above
scan_node = head_node
while i < index and scan_node.next:
scan_node = scan_node.next
i += 1
# here scan_node.next is the node we want removed, or None
if scan_node.next:
scan_node.next = scan_node.next.next # jump over the removed node
return head_node
您没有修改对象;您正在替换存储在匿名数组中的对象。
foo
的实际和可疑输出是什么?我怀疑您所需要做的就是进行递归调用foo(a.next,b-1)
;您正在替换存储在匿名数组中的对象。foo
的实际和可疑输出是什么?我猜想你所需要做的就是进行递归调用foo(a.next,b-1)
。我真的在尝试模拟void foo(node**obj){*obj=(*obj)->next;}(C++)实际上回答了我的问题。虽然这很巧妙,但这只是使两个对象共享\u dict\u
,所以一个对象中的更改会反映在另一个对象中。不鼓励直接操作\u dict\u
;允许它为None
(请参阅您的getattr
调用)可能是灾难性的。你说得对。也许使用deepcopy复制\uuuuu dict\uuuu
可以稍微提高一点。至于None问题,代码应该根据它们的情况进行修改。虽然这很巧妙,但这只是使两个对象共享\uuuu dict\uuuu
,因此一个对象中的更改会反映在另一个对象中。不鼓励直接操作\u dict\u
;允许它为None
(请参阅您的getattr
调用)可能是灾难性的。你说得对。也许使用deepcopy复制\uuuuu dict\uuuu
可以稍微提高一点。至于None问题,应该根据它们的情况修改代码。