Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:35:46+00:00 2026-06-05T14:35:46+00:00

Is there a function in the Python standard library to reproduce Python’s name mangling

  • 0

Is there a function in the Python standard library to reproduce Python’s name mangling scheme with a “private” attribute name? It seems like there would be, but I can’t find it for the life of me.

I wrote this, but if there’s a better way I’m all ears.

def mangle_name (cls, attrname) :
    prefix = '_' + cls.__name__.lstrip('_')

    if not attrname.startswith('__') :
        attrname = '__' + attrname

    if not attrname.endswith('__') :
        return prefix + attrname
    else :
        return attrname

class Foo :
    __some_such = 3

name = mangle_name(Foo, '__some_such')
print name
print hasattr(Foo(), name)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:35:48+00:00Added an answer on June 5, 2026 at 2:35 pm

    It looks like the compiler module has a Python implementation for this, the signature is mangle(name, klass) where klass is the class name, not the object itself.

    Here is how you can access and use it:

    >>> from compiler.misc import mangle
    >>> mangle('__some_such', 'Foo')
    '_Foo__some_such'
    

    Note that the compiler module is deprecated since Python 2.6 and does not exist in Python 3.0.

    Here is the function itself (from Python 2.7 source code) in case you just want to copy it into your source or verify that your version is equivalent:

    MANGLE_LEN = 256 # magic constant from compile.c
    
    def mangle(name, klass):
        if not name.startswith('__'):
            return name
        if len(name) + 2 >= MANGLE_LEN:
            return name
        if name.endswith('__'):
            return name
        try:
            i = 0
            while klass[i] == '_':
                i = i + 1
        except IndexError:
            return name
        klass = klass[i:]
    
        tlen = len(klass) + len(name)
        if tlen > MANGLE_LEN:
            klass = klass[:MANGLE_LEN-tlen]
    
        return "_%s%s" % (klass, name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there python function in standard library like def cond(condition, true, false): if condition:
In python, there is a function called getattr which would look like this: class
I am wondering if there is a standard library function in Python which will
Is there standard function to enumerate an F# sequence that works like Python's enumerate()
Is there a built-in function for this in Python 2.6? Something like: clamp(myValue, min,
Is there a standard function for Python which outputs True or False probabilistically based
Is there a standard function that will convert http headers into a python dictionary,
is there a similar function in python that takes search(array) and replace(array) as a
There is an eval() function in Python I stumbled upon while playing around. I
Is there any build-in function in Python that merges two lists into a dict?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.