[ python-Bugs-1539847 ] Identifiers begining with __ renamed

SourceForge.net noreply at sourceforge.net
Wed Aug 16 04:10:04 CEST 2006


Bugs item #1539847, was opened at 2006-08-14 15:47
Message generated for change (Comment added) made by w_barnes
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1539847&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.4
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: W Barnes (w_barnes)
Assigned to: Nobody/Anonymous (nobody)
Summary: Identifiers begining with __ renamed

Initial Comment:
Identifiers used in a class function that start with 
__ are renamed to _classname__identifer even if the 
identifier is owned by some other object.

Code snippet:

size = len(Data.__entry_dates)

Here I'm trying to access the identifier 
__entry_dates in the module Data from the function 
DataTestCase.testEntryDates() but I get the following:

Traceback (most recent call last):
  File "DataTest.py", line 247, in testEntryDates
    size = len(Data.__entry_dates)
AttributeError: 'module' object has no 
attribute '_DataTestCase__entry_dates'

I'm using Python 2.4.3 on Windows XP sp2

Thanks,
Walter

----------------------------------------------------------------------

>Comment By: W Barnes (w_barnes)
Date: 2006-08-16 10:10

Message:
Logged In: YES 
user_id=1541460

Thanks!

I was unaware that name mangling applied to global 
attributes as well as class attributes.

The error message is a bit misleading though. If I define 
__widget in foo then access it from bar as foo.__widget 
why does it rename it as _bar__widget if it was not 
defined there? If it needs to do this internally why not 
use the unmangled name for the error message?

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2006-08-14 16:07

Message:
Logged In: YES 
user_id=849994

This is exactly how __ name mangling is supposed to work.
These are meant to be private, and thus shouldn't be
accessed from another class.

If the attribute was in another class, you could do the
mangling (with the correct class name!) yourself, as it's on
a module in this case, use __dict__:
size = len(Data.__dict__['__entry_dates'])

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1539847&group_id=5470


More information about the Python-bugs-list mailing list