简体   繁体   中英

Passing object parameter by value

Code:

class Stack:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

def length(stack):
    i = 0
    while not stack.is_empty():
        stack.pop()
        i += 1
    return i

s1 = Stack()
s1.push(3)
s1.push(2)
s1.push(1)
print(length(s1))
s1.pop()

Output:

3
Traceback (most recent call last):
  File "Stack.py", line 26, in <module>
    s1.pop()
  File "Stack.py", line 12, in pop
    return self.items.pop()
IndexError: pop from empty list

I want the function length() to be able to modify a copy of s1 instead on changing s1 . Is there any way to do this in python?

I am not allowed to directly use s1.items so I can't just use s1[:] . I can't modify the class either.

You could simply use the copy module:

import copy

# ... your code ...

print(length(copy.deepcopy(s1)))  # pass a copy to the length function

Or if you want it without extra module and if you can alter the length function you could simply keep the pop ped items and push them again after you have the length:

def length(stack):
    i = 0
    tmp = []
    while not stack.is_empty():
        tmp.append(stack.pop())    # append them to your temporary storage
        i += 1
    for item in tmp:               # take the items saved in the temporary list
        stack.push(item)           # and push them into your stack again
    return i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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