Skip to main content
Active reading [<https://en.wiktionary.org/wiki/parameter#Noun> <https://en.wiktionary.org/wiki/instead#Adverb>]. Changed to sentence casing for the title.
Source Link
Peter Mortensen
  • 31.6k
  • 22
  • 110
  • 133

Python Public and Private Methodsprivate methods in Python

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

self.__top = None # <-- In stead of self.top = None

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

Python Public and Private Methods

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

self.__top = None # <-- In stead 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

Public and private methods in Python

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
Post Closed as "Duplicate" by Prune python
added 56 characters in body
Source Link
Reedinationer
  • 5.8k
  • 1
  • 14
  • 33

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

self.__top = None # <-- In stead 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:

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

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

self.__top = None # <-- In stead 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

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

self.__top = None # <-- In stead 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
Source Link
NiRvanA
  • 135
  • 1
  • 1
  • 8

Python Public and Private Methods

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

self.__top = None # <-- In stead 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