473,404 Members | 2,114 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,404 software developers and data experts.

__Classes and type tests

Hi all,

The code below exhibits an attempt to refer to the type of a __Class
from within a method of that class. I've been unable to figure out how
to make it work as I want, and would appreciate any insight.

The problem emerged out of a bad design that the good folks on the
tutor list helped me repair. With that repair, I no longer need nor
want to do this sort of thing in actual code. But the academic issue
"How/Can it be done?" still itches.
class _OneUnderBase(object):
def __init__(self):
if type(self) == _OneUnderBase:
print "From _OneUnderBase"
else:
print "From subclass",

class __TwoUnderBase(object):
def __init__(self):
# What to write in the if type() test to secure same
# behaviour as _OneUnderBase
if type(self) == __TwoUnderBase:
print "From __TwoUnderBase"
else:
print "From subclass",

class SubOne(_OneUnderBase):
def __init__(self):
super(SubOne, self).__init__()
print "SubOne"

class SubTwo(__TwoUnderBase):
def __init__(self):
super(SubTwo, self).__init__()
print "SubTwo"

s1 = SubOne()
s2 = SubTwo()
When run, this gives:
From subclass SubOne
Traceback (most recent call last):
File "C:/Documents and Settings/Owner/My
Documents/PythonFiles/foo.py", line 28, in ?
s2 = SubTwo()
File "C:/Documents and Settings/Owner/My
Documents/PythonFiles/foo.py", line 24, in __init__
super(SubTwo, self).__init__()
File "C:/Documents and Settings/Owner/My
Documents/PythonFiles/foo.py", line 12, in __init__
if type(self) == __TwoUnderBase:
NameError: global name '_TwoUnderBase__TwoUnderBase' is not defined


So, the _OneUnderBase version works fine. I've been unable to work out
what to write in the __TwoUnderBase version to make it work the same.

(In case it is not clear, I'm aiming at the output:
From subclass SubOne
From subclass SubTwo
)

I *think* I understand how __ name mangling works when making
references from one module to another. But I cannot work it out
reference to the class itself from within a __ class.

Should it matter: Python 2.4.2 on Win32.

Thanks,

Brian vdB

Oct 9 '05 #1
1 1076
Brian van den Broek wrote:
The code below exhibits an attempt to refer to the type of a __Class
from within a method of that class. I've been unable to figure out how
to make it work as I want, and would appreciate any insight.

The problem emerged out of a bad design that the good folks on the
tutor list helped me repair. With that repair, I no longer need nor
want to do this sort of thing in actual code. But the academic issue
"How/Can it be done?" still itches.


Define the method outside the class to defeat mangling:
def __init__(self): .... if type(self) == __Two:
.... print "two leading underscores"
.... class __Two(object): .... __init__ = __init__
.... __Two()

two leading underscores
<__main__.__Two object at 0x4029360c>

Peter

Oct 10 '05 #2

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

Similar topics

2
by: burdeen | last post by:
I've been trying to return an array with PHP5 soap. Is this not supported? or am i doing it wrong? Seems to return on the last element in the array. In my WSDL i've defined my return type as a...
12
by: Aaron Watters | last post by:
I'm doing a heart/lung bypass procedure on a largish Python program at the moment and it prompted the thought that the methodology I'm using would be absolutely impossible with a more "type safe"...
3
by: TPJ | last post by:
"The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all...
16
by: Greg Roberts | last post by:
Hi I want to place the tests needed in the code using attributes. There seems to be enough code snippets around for me to cover this. e.g. // Test cases, run these here on the function and...
2
by: um | last post by:
When the POSIX pthreads library for w32 release 2-2-0 (http://sources.redhat.com/pthreads-win32/) is compiled with VC++6 then it compiles and passes all the benchmark tests in the subdirectory...
6
by: Ben Finney | last post by:
Howdy all, Summary: I'm looking for idioms in unit tests for factoring out repetitive iteration over test data. I explain my current practice, and why it's unsatisfactory. When following...
6
by: Jeremy | last post by:
I understand what a unit test is. No problem there. What I'm wondering is what, specifically, do most of you more experienced developers mean by "writing" a unit test. Am I correct to believe...
4
by: Gianni Mariani | last post by:
Below is an attempt to factorize the "SFINAE" idiom. I have tried it on three compilers and they all complain. As far as I can tell, it's valid code. The questions are, are the compilers right...
270
by: Jordan | last post by:
Hi everyone, I'm a big Python fan who used to be involved semi regularly in comp.lang.python (lots of lurking, occasional posting) but kind of trailed off a bit. I just wrote a frustration...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.