The Tech Edvocate

Top Menu

  • Advertisement
  • Apps
  • Home Page
  • Home Page Five (No Sidebar)
  • Home Page Four
  • Home Page Three
  • Home Page Two
  • Home Tech2
  • Icons [No Sidebar]
  • Left Sidbear Page
  • Lynch Educational Consulting
  • My Account
  • My Speaking Page
  • Newsletter Sign Up Confirmation
  • Newsletter Unsubscription
  • Our Brands
  • Page Example
  • Privacy Policy
  • Protected Content
  • Register
  • Request a Product Review
  • Shop
  • Shortcodes Examples
  • Signup
  • Start Here
    • Governance
    • Careers
    • Contact Us
  • Terms and Conditions
  • The Edvocate
  • The Tech Edvocate Product Guide
  • Topics
  • Write For Us
  • Advertise

Main Menu

  • Start Here
    • Our Brands
    • Governance
      • Lynch Educational Consulting, LLC.
      • Dr. Lynch’s Personal Website
      • Careers
    • Write For Us
    • The Tech Edvocate Product Guide
    • Contact Us
    • Books
    • Edupedia
    • Post a Job
    • The Edvocate Podcast
    • Terms and Conditions
    • Privacy Policy
  • Topics
    • Assistive Technology
    • Child Development Tech
    • Early Childhood & K-12 EdTech
    • EdTech Futures
    • EdTech News
    • EdTech Policy & Reform
    • EdTech Startups & Businesses
    • Higher Education EdTech
    • Online Learning & eLearning
    • Parent & Family Tech
    • Personalized Learning
    • Product Reviews
  • Advertise
  • Tech Edvocate Awards
  • The Edvocate
  • Pedagogue
  • School Ratings

logo

The Tech Edvocate

  • Start Here
    • Our Brands
    • Governance
      • Lynch Educational Consulting, LLC.
      • Dr. Lynch’s Personal Website
        • My Speaking Page
      • Careers
    • Write For Us
    • The Tech Edvocate Product Guide
    • Contact Us
    • Books
    • Edupedia
    • Post a Job
    • The Edvocate Podcast
    • Terms and Conditions
    • Privacy Policy
  • Topics
    • Assistive Technology
    • Child Development Tech
    • Early Childhood & K-12 EdTech
    • EdTech Futures
    • EdTech News
    • EdTech Policy & Reform
    • EdTech Startups & Businesses
    • Higher Education EdTech
    • Online Learning & eLearning
    • Parent & Family Tech
    • Personalized Learning
    • Product Reviews
  • Advertise
  • Tech Edvocate Awards
  • The Edvocate
  • Pedagogue
  • School Ratings
  • Miro vs Conceptboard for project management

  • How to use Teamwork for resource management

  • How much does Procore cost per month

  • How to organize Microsoft Planner buckets

  • Is Teamwork good for billable hours

  • Procore vs Buildertrend which is better

  • Airtable project management templates

  • Can Miro track project progress

  • How to use Basecamp for client work

  • Can Lucidchart create swimlane diagrams

How To
Home›How To›4 Ways to Call Private Methods in Python

4 Ways to Call Private Methods in Python

By Matthew Lynch
December 14, 2023
0
Spread the love

Introduction:

Private methods are often used as an implementation detail and are not meant to be accessed directly by the users of a class. The name mangling mechanism in Python makes it difficult to call private methods from outside the class. However, there are times when developers may need to access these methods for debugging or testing purposes. In this article, we will explore four different ways to call private methods in Python.

1. Direct Access:

Despite their ‘private’ nature, you can still access private methods just as you would any other method by using the mangling notation. Consider the following example:

“`python

class MyClass:

def __init__(self):

pass

def _MyClass__private_method(self):

print(“This is a private method”)

my_object = MyClass()

my_object._MyClass__private_method() # This will call the private method
“`

In this example, `_MyClass__private_method` is used to call the private method named `__private_method`. Keep in mind that accessing a private method this way goes against common best practices.

2. Using the `getattr()` Function:

Python’s built-in function `getattr()` allows you to access attributes of an object knowing their names as strings.

Here’s how you could use this function to call a private method:

“`python

class MyClass:

def __init__(self):

pass

def __private_method(self):

print(“This is a private method”)

my_object = MyClass()

method_name = ‘_MyClass__private_method’

getattr(my_object, method_name)() # This will also call the private method
“`

3. Using an Intermediate Public Method:

You can also create a public method inside the class that calls the private method and then access the public method from outside the class:

“`python

class MyClass:

def __init__(self):

pass

def __private_method(self):

print(“This is a private method”)

def public_method(self):

self.__private_method()

my_object = MyClass()

my_object.public_method() # This will call the private method via the public method
“`

This approach does not break the encapsulation principle and provides a controlled way to access private methods.

4. Monkey Patching:

Monkey patching is a technique where you modify or extend a module or class at runtime. You can use monkey patching to define an alternative way of accessing a private method:

“`python

class MyClass:

def __init__(self):

pass

def __private_method(self):

print(“This is a private method”)

def call_private_method(obj):

obj._MyClass__private_method()

MyClass.call_private = call_private_method

my_object = MyClass()

my_object.call_private() # This will call the private method via monkey patching
“`
Conclusion:

It’s essential to understand that accessing private methods directly from outside the class should be avoided, as it breaks the encapsulation principle and makes your code less maintainable. However, these four ways of calling private methods may be useful for debugging and testing purposes or when you need to access external libraries that don’t provide enough public interfaces. Always proceed with caution when using such techniques.

Previous Article

How to Be Free from Sin

Next Article

3 Ways to Use Oregano Oil

Matthew Lynch

Related articles More from author

  • How To

    3 Ways to Improve Your Leg Flexibility

    October 17, 2023
    By Matthew Lynch
  • How To

    3 Ways to Fix Ripped Jeans

    October 16, 2023
    By Matthew Lynch
  • How To

    3 Ways to Clean the Inside of a Slow Cooker

    February 7, 2024
    By Matthew Lynch
  • How To

    4 Ways to Study Engineering

    April 3, 2024
    By Matthew Lynch
  • How To

    4 Ways to Say “You’re Welcome” in French

    April 2, 2024
    By Matthew Lynch
  • How To

    4 Ways to Tell if Milk is Bad

    November 3, 2023
    By Matthew Lynch

Search

Login & Registration

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Newsletter

Signup for The Tech Edvocate Newsletter and have the latest in EdTech news and opinion delivered to your email address!

About Us

Since technology is not going anywhere and does more good than harm, adapting is the best course of action. That is where The Tech Edvocate comes in. We plan to cover the PreK-12 and Higher Education EdTech sectors and provide our readers with the latest news and opinion on the subject. From time to time, I will invite other voices to weigh in on important issues in EdTech. We hope to provide a well-rounded, multi-faceted look at the past, present, the future of EdTech in the US and internationally.

We started this journey back in June 2016, and we plan to continue it for many more years to come. I hope that you will join us in this discussion of the past, present and future of EdTech and lend your own insight to the issues that are discussed.

Newsletter

Signup for The Tech Edvocate Newsletter and have the latest in EdTech news and opinion delivered to your email address!

Contact Us

The Tech Edvocate
910 Goddin Street
Richmond, VA 23231
(601) 630-5238
[email protected]

Copyright © 2026 Matthew Lynch. All rights reserved.