1

I have a class Stack in which I implemented a function which reverses a string.

class Stack:

        def __init__(self,x):
            self.__items = []


        def reverse(self):
            if len(self.__items ) <= 1:
                return self.__items
            else:
                return reverse(self.__items [1:]) + self.__items [0]        



word=input("Please enter word to reverse: ")
list_word=list(word)
rev=Stack.reverse(list_word)
print(rev)

I have a fully functioning reverse function but yet my class returns me an Attribute Error. Thoughts? Do I need a for loop?

2 Answers 2

1

If you want to call this as a static method, the following should work:

@staticmethod
def reverse(the_list):
    if len(the_list) <= 1:
        return the_list
    else:
        return reverse(the_list[1:]) + [the_list[0]]

Ultimately though, I think you're misunderstanding the differences between static methods and instance methods.

Instance Methods

Instance methods can only be called on an instance of an object. In the case of your Stack class, you would have to call the method like so:

stack = Stack()
stack.reverse([1, 2, 3, 4])

First, we have to create an instance of the Stack class, and then we call its instance method reverse. With an instance method, the method signature for reverse will look like the following:

def reverse(self, the_list):

Note that there are two arguments listed in the method signature, even though we only passed it one argument when we called it above. This is because, for an instance method, there is an implicit self argument that represents the instance of the object that we're calling the method on. Any additional parameters will be bound to the subsequent arguments. Our list, [1, 2, 3, 4], is bound to the the_list parameter of the function.

Contrast this with...

Static Methods

A static method is a method attached to the class itself, rather than an instance of that class. Thus, you don't need an instance in order to call the method. That's what you've got in your question:

#calling the method statically
Stack.reverse([1, 2, 3, 4])

Within the Stack class, the method signature for reverse will look like the following:

@staticmethod
def reverse(the_list):

Note the difference: there's no self parameter anymore. This is because a static method is just like a normal function, but it's attached to a class. Because it's a normal function, there's no object instance associated with it and, therefore, no self parameter.

Here's a better explanation: https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-methods

Sign up to request clarification or add additional context in comments.

1 Comment

well it needs self.items since its in a class
0

stack.reverse is not a static method. you have to define a stack object and run the function on the object. For future reference, you can use python's @staticmethod keyword on top of the method if it doesn't depend on any class variables to treat it like any other function.

2 Comments

Making reverse a staticmethod will not work in this case. The method needs access to self.__items. Remember that staticmethods do not receive a self argument.
true, should have taken a closer look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.