473,385 Members | 1,983 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,385 software developers and data experts.

Accessing 'mangled' class attrbutes

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 ).flush()
super( HtmlWriter, self ).__write(text)

but because of the name-mangling caused by '__write' I get:

AttributeError: 'super' object has no attribute '_HtmlWriter__write'.

Is there any simple way round this situation in general?

(I just want to write out a HTML 'DOCTYPE' declaration)

Thanks

Gerard

Mar 1 '06 #1
6 1403
Gerard Flanagan wrote:
I would like to do the following:

from elementtree.SimpleXMLWriter import XMLWriter

class HtmlWriter(XMLWriter, object):
def write_raw(self, text):
super( HtmlWriter, self ).flush()
super( HtmlWriter, self ).__write(text)

but because of the name-mangling caused by '__write' I get:

AttributeError: 'super' object has no attribute '_HtmlWriter__write'.

Is there any simple way round this situation in general?

(I just want to write out a HTML 'DOCTYPE' declaration)


Try: (not the Python keyword, but a directive to you)

super(HtmlWriter, self)._XMLWriter__write(text)

In general, to access the name-mangled members, simply add
_<class_name> to the front of the member name and you should be able to get
at it. But be careful, since this is a reference to the base class, so if
it's inherited from some other class, you'll need to know from which class
the member is inherited.

HTH

--
Steve Juranich
Tucson, AZ
USA

Mar 1 '06 #2
>>Is there any simple way round this situation in general?

It might be safer to use composition instead of inheritance in this
case. Assuming that XMLWriter has a write method to write what you
want, you could hold a reference to an XMLWriter within your class and
pass along write command like:

writer = XMLWriter()
writer.write(stuff)

Mar 1 '06 #3

Steve Juranich wrote:
Gerard Flanagan wrote:
I would like to do the following:

from elementtree.SimpleXMLWriter import XMLWriter

class HtmlWriter(XMLWriter, object):
def write_raw(self, text):
super( HtmlWriter, self ).flush()
super( HtmlWriter, self ).__write(text)

but because of the name-mangling caused by '__write' I get:

AttributeError: 'super' object has no attribute '_HtmlWriter__write'.

Is there any simple way round this situation in general?

(I just want to write out a HTML 'DOCTYPE' declaration)


Try: (not the Python keyword, but a directive to you)

super(HtmlWriter, self)._XMLWriter__write(text)

In general, to access the name-mangled members, simply add
_<class_name> to the front of the member name and you should be able to get
at it. But be careful, since this is a reference to the base class, so if
it's inherited from some other class, you'll need to know from which class
the member is inherited.

HTH

--
Steve Juranich
Tucson, AZ
USA


I tried that Steve but it didn't work, and i don't think I can do what
I want in any case. There is no method '__write' in the base class, it
is only declared as an instance attribute in the constructor, like so:

def __init__(self, file, encoding="us-ascii"):
...
self.__write = file.write
...

I tried putting '__write = None' at the class level (in the base class
XMLWriter) but then, although '_XMLWriter__write' appears in
'dir(HtmlWriter)', I get 'NoneType is not callable'.

I also tried 'def __write(self, text) : pass ' in the base class, but
then the code runs but doesn't write the text I want - and anyway, if
I'm going to change the base class, then i may as well just add the
'write_raw' method to the base directly!

It's just some toy code at any rate, and I've learnt something new!
Thanks for your reply.

Gerard

Mar 1 '06 #4
da********@yahoo.com wrote:
Is there any simple way round this situation in general?


It might be safer to use composition instead of inheritance in this
case. Assuming that XMLWriter has a write method to write what you
want, you could hold a reference to an XMLWriter within your class and
pass along write command like:

writer = XMLWriter()
writer.write(stuff)


No, XMLWriter doesn't have a 'write' method, if it did I could have
done:

super(HtmlWriter, self).write(stuff)

I think the XMLWriter class has been designed so you can't just write
any old text because this better ensures that tags are properly closed
and so on. There is a public 'data' method:

writer.data( text )

but it escapes angle brackets, and what i wanted was to write
'<!DOCTYPE.....'.

Thanks

Gerard

Mar 1 '06 #5
Gerard Flanagan wrote:
I tried that Steve but it didn't work, and i don't think I can do what
I want in any case. There is no method '__write' in the base class, it
is only declared as an instance attribute in the constructor, like so:

def __init__(self, file, encoding="us-ascii"):
...
self.__write = file.write
...

I tried putting '__write = None' at the class level (in the base class
XMLWriter) but then, although '_XMLWriter__write' appears in
'dir(HtmlWriter)', I get 'NoneType is not callable'.

I also tried 'def __write(self, text) : pass ' in the base class, but
then the code runs but doesn't write the text I want - and anyway, if
I'm going to change the base class, then i may as well just add the
'write_raw' method to the base directly!

It's just some toy code at any rate, and I've learnt something new!
Thanks for your reply.

Gerard


Make sure you're calling the super's constructor before you try and access
the mangled member. Then (I forgot this part), you can just call the
mangled member from `self'. Example follows.

<foo.py>
class A(object):
def __init__(self):
self.__foo = lambda x, y : x + y

class B(A):
def __init__(self, x, y):
# Make sure you're calling the super's constructor first.
super(B, self).__init__()
self.sum = self._A__foo(x, y)
</foo.py>
import foo
b = foo.B(3, 4)
b.sum 7


--
Steve Juranich
Tucson, AZ
USA

Mar 1 '06 #6
Steve Juranich wrote:
Gerard Flanagan wrote:
I tried that Steve but it didn't work, and i don't think I can do what
I want in any case. There is no method '__write' in the base class, it
is only declared as an instance attribute in the constructor, like so:

def __init__(self, file, encoding="us-ascii"):
...
self.__write = file.write
...

I tried putting '__write = None' at the class level (in the base class
XMLWriter) but then, although '_XMLWriter__write' appears in
'dir(HtmlWriter)', I get 'NoneType is not callable'.

I also tried 'def __write(self, text) : pass ' in the base class, but
then the code runs but doesn't write the text I want - and anyway, if
I'm going to change the base class, then i may as well just add the
'write_raw' method to the base directly!

It's just some toy code at any rate, and I've learnt something new!
Thanks for your reply.

Gerard


Make sure you're calling the super's constructor before you try and access
the mangled member. Then (I forgot this part), you can just call the
mangled member from `self'. Example follows.

<foo.py>
class A(object):
def __init__(self):
self.__foo = lambda x, y : x + y

class B(A):
def __init__(self, x, y):
# Make sure you're calling the super's constructor first.
super(B, self).__init__()
self.sum = self._A__foo(x, y)
</foo.py>
import foo
b = foo.B(3, 4)
b.sum 7


--
Steve Juranich
Tucson, AZ
USA

It's all becoming clear! Yes, calling the base constructor was all I
needed to do:

class HtmlWriter(elementtree.SimpleXMLWriter.XMLWriter, object):

def __init__(self, file):
super( HtmlWriter, self).__init__(file)

def write_raw(self, text):
self.flush()
self._XMLWriter__write(text)

-works a charm. Appreciate your help Steve, thanks again.

Gerard

Mar 1 '06 #7

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

Similar topics

5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
5
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
0
by: seek help | last post by:
Hello, IDE: VS .NET 2003. Problem: Mangled bits of gc class embedded within native C++ class. Description: I have a gc class, BoxedInfo. This wraps a Value type structure, NotiInfo....
1
by: Peter J. Bismuti | last post by:
How do you access attributes of a class when inheriting from it? Can't you just say: self.attribute? Help?!...
5
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the...
2
by: toduro | last post by:
Sorry about the bad subject line earlier. What is the right syntax for a C++ declaration which would give me __ls__7ostreamPFR7ostream_R7ostream in an object file produced by the gcc 2.95...
49
by: comp.lang.php | last post by:
/** * Class for grayscaling image * * @author Phil Powell * @version 1.2.1 * @package IMAGE_CATALOG::IMAGE */ class ImageGrayscaleGenerator extends ImageResizeComponents { /
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?

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.