Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Public and private methods in Python [duplicate]

I am trying to change a public to private parameter inside a Python class. As far as I know, to make it private I need to declare for instance:

self.__top = None # <-- Instead of self.top = None

However, I cannot figure out how to make a private struct with private properties, such that they are called properly in the public methods.

For instance:

class Stack:

    def __init__(self, size=None):
        self.__top = None
        if size is None:  # if size is unset
            self.__size = -1
        else:
            self.__size = size
        self.__current_size = -1

    def push(self, data):
        if self.current_size >= self.size: # ERROR!
            print("Stack Overflow!")
            return

Answer*

Cancel