473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

puzzled about class attribute resolution and mangling

Hi all,

I've the following code snippet that puzzles me:

class Base(object):
__v, u = "Base v", "Base u"
def __init__(self):
print self.__v, self.u

class Derived(Base):
__v, u = "Derived v", "Derived u"
def __init__(self):
print self.__v, self.u
super(Derived, self).__init__()

d = Derived()

When run (Python 2.4.2, IDLE 1.1.2), it produces:
Derived v Derived u
Base v Derived u


What I expected was that all four emitted strings would contain "Derived".

I conclude that there is something about the cluster of concepts at
hand this hobbyist doesn't understand :-) I suspect that the problem
is with my understanding of the name mangling mechanism, but then
again, I'm the confused one.

I'd thought the point of the mangling was to make it sufficiently
difficult for client code to access the mangled name so as to
constitute a strong recommendation to leave the name alone. But, since
the access is all from within method code, I didn't expect any
mangling issues here. Since d is a Derived, I expected any method of d
trying to find d.__v to first check if there is a Derived.__v and only
then pass to Base.__v. Obviously, that's not what's happening.

So, is this behaviour entirely by design and my surprise entirely the
product of misconception or is there an element of side effect of the
mangling mechanism at issue? Or some other consideration altogether?

Thanks and best,

Brian vdB
Dec 10 '05 #1
2 1133
Brian van den Broek wrote:
Hi all,

I've the following code snippet that puzzles me:

class Base(object):
__v, u = "Base v", "Base u"
def __init__(self):
print self.__v, self.u

class Derived(Base):
__v, u = "Derived v", "Derived u"
def __init__(self):
print self.__v, self.u
super(Derived, self).__init__()

d = Derived()

When run (Python 2.4.2, IDLE 1.1.2), it produces:
>>> Derived v Derived u
Base v Derived u >>>


What I expected was that all four emitted strings would contain "Derived".

I conclude that there is something about the cluster of concepts at
hand this hobbyist doesn't understand :-) I suspect that the problem is
with my understanding of the name mangling mechanism, but then again,
I'm the confused one.

I'd thought the point of the mangling was to make it sufficiently
difficult for client code to access the mangled name so as to
constitute a strong recommendation to leave the name alone. But, since
the access is all from within method code, I didn't expect any mangling
issues here. Since d is a Derived, I expected any method of d trying to
find d.__v to first check if there is a Derived.__v and only then pass
to Base.__v. Obviously, that's not what's happening.

So, is this behaviour entirely by design and my surprise entirely the
product of misconception or is there an element of side effect of the
mangling mechanism at issue? Or some other consideration altogether?

Thanks and best,

Brian vdB


This is name mangling at work. Mangling turns self.__v in the Derrived class's
__init__ method to self._Derrived__v and self.__v in the Base class's __init__
method to self._Base__v. These are different names bound to different values and
are reflected as such. self.u is the same name in both cases and the value was
bound in the Derrived class, and not re-bound in the Base class.

James
Dec 10 '05 #2
James Stroud said unto the world upon 2005-12-09 20:39:
Brian van den Broek wrote:
Hi all,

I've the following code snippet that puzzles me:

class Base(object):
__v, u = "Base v", "Base u"
def __init__(self):
print self.__v, self.u

class Derived(Base):
__v, u = "Derived v", "Derived u"
def __init__(self):
print self.__v, self.u
super(Derived, self).__init__()

d = Derived()

When run (Python 2.4.2, IDLE 1.1.2), it produces:
>>>

Derived v Derived u
Base v Derived u
>>>


What I expected was that all four emitted strings would contain "Derived".
<snip me -- Brian -- speculating on locus of my confusion>
Thanks and best,

Brian vdB

This is name mangling at work. Mangling turns self.__v in the Derrived class's
__init__ method to self._Derrived__v and self.__v in the Base class's __init__
method to self._Base__v. These are different names bound to different values and
are reflected as such. self.u is the same name in both cases and the value was
bound in the Derrived class, and not re-bound in the Base class.

James


Thanks for the reply, James. Rereading the relevant section of the
Tutorial:
Any identifier of the form __spam (at least two leading
underscores, at most one trailing underscore) is textually replaced
with _classname__spam, where classname is the current class name
with leading underscore(s) stripped.

<http://docs.python.org/tut/node11.html#SECTION0011600000000000000000>

I'm not sure how I came to think that names were mangled only with
respect to calling code from outside class definitions.

As I'd been confused, I naturally started thinking of ways to clarify
the docs. But, I've come to think that the error was wholly mine.

Thanks for the help,

Brian vdB
Dec 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jeffrey Borkent | last post by:
Hi There, Please let me rephrase the problem as many people misunderstood what i was trying to ask. I know that the __init__ is the class constructor, that was not my question. under the...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
12
by: christopherlmarshall | last post by:
Suppose you want to write a subclass of some existing class you are importing from a module you didn't write and that you don't want to study the internals of, and you want to define a data member...
6
by: Gerard Flanagan | last post by:
Hello all I would like to do the following: from elementtree.SimpleXMLWriter import XMLWriter class HtmlWriter(XMLWriter, object): def write_raw(self, text): super( HtmlWriter, self...
4
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
6
by: =?Utf-8?B?QkJN?= | last post by:
Hi, I have an app that is crashing due to a System.ArgumentException. At this point it's just a simple app to test some basic object values. The main app is a Windows App that looks like...
3
by: antred | last post by:
Hello everyone, While working on a program I encountered a situation where I'd construct a largish data structure (a tree) from parsing a host of files and would end up having to throw away...
2
by: tron.thomas | last post by:
The following code will print a message only once: class PrintOnce: printOnce = True def __init__(self): if PrintOnce.printOnce: print 'Printing once.' PrintOnce.printOnce = False first =...
8
by: Brian Munroe | last post by:
My example: class A(object): def __init__(self, name): self.__name = name def getName(self): return self.__name
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.