473,404 Members | 2,195 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.

need help on sublcass and scope

I'm an OOP newbie, and needs help on subclassing from different module.

I made a base module a.py which contains two classes C1 and C2;

## start of a.py

class C1(object):
def m(self):
print "method m in class C1 in module a"

class C2(object):
def __init__(self):
print "class C2 in module a"
self.a = C1()
self.a.m()

## end of of a.py

Then, I made another module b.py which extends this base module;

## start of b.py

import a

class C1(a.C1):
def m(self):
print "method m in class C1 in module b"
a.C1.m(self)

class C2(a.C2):
def __init__(self):
print "class C2 in module b"
a.C2.__init__(self)

## end of of b.py

When I instantiate C2, I get;
import b
i = b.C2() class C2 in module b
class C2 in module a
method m in class C1 in module a
It doesn't use class C1 in module 'b', but uses C1 in module 'a' because
the last line in b.py 'a.c2.__init__(self)' runs with module scope a.
So I tweaked the C1 instantiation line in a.py from

self.a = C1()

to

import sys
self.a = sys.modules[self.__module__].C1()

and got the result I expected;
import b
i = b.C2() class C2 in module b
class C2 in module a
method m in class C1 in module b
method m in class C1 in module a


but it looks like an ugly hack to me.
If there's common OOP idiom to handle this kind of problem, give me
some pointer.

Thanks,
Inyeol

Jul 18 '05 #1
1 1321
Inyeol Lee wrote:
I'm an OOP newbie, and needs help on subclassing from different module.

I made a base module a.py which contains two classes C1 and C2;
[...]
but it looks like an ugly hack to me.
If there's common OOP idiom to handle this kind of problem, give me
some pointer.


I think with all these a, b and Cs, you raised the abstraction level too
high. To me - at least - it remains unclear what you are trying to achieve.

I've made a bold guess and tweaked your example to output what you expected
without having to mess with sys.modules.

#a.py
class C1(object):
def m(self):
print "method m in class C1 in module a"

class C2(object):
def __init__(self, a=None):
print "class C2 in module a"
if a is None: a = C1()
self.a = a
self.a.m()
#b.py
import a

class C1(a.C1):
def m(self):
print "method m in class C1 in module b"
a.C1.m(self)

class C2(a.C2):
def __init__(self):
print "class C2 in module b"
a.C2.__init__(self, C1())

If you want to modify a part of the C2 implementation independently of the
C2 hierarchy, you could make C1 a Mixin:

#a.py vs 2
class C1(object):
def m(self):
print "method m in class C1 in module a"

class C2(object):
def __init__(self, a=None):
print "class C2 in module a"
self.m()

#b.py vs 2
import a
class C1(a.C1):
def m(self):
print "method m in class C1 in module b"
super(C1, self).m()

class C2(a.C2, C1):
def __init__(self):
print "class C2 in module b"
super(C2, self).__init__()

The beauty of this approach becomes visible if you add further subclasses to
C1:

#c.py aka b.py vs 3
import a
class C11(a.C1):
def m(self):
print "method m in class C11 in module c"
super(C11, self).m()

class C12(a.C1):
def m(self):
print "method m in class C12 in module c"
super(C12, self).m()

class C2(a.C2, C11, C12):
def __init__(self):
print "class C2 in module b"
super(C2, self).__init__()

a.C1 occurs twice in the hierarchy, so how often would you expect
"method m in class C1 in module a" to be printed?
I you anser 1, then which of the following messages will be omitted:
"method m in class C11 in module c" or "method m in class C12 in module c"

Let's try:
import c
c.C2() class C2 in module b
class C2 in module a
method m in class C11 in module c
method m in class C12 in module c
method m in class C1 in module a
<c.C2 object at 0x40295f0c>


Every m() was exactly called once!

However, I'm not an OOP newbie and still have doubts if I've got the above
right, so I would recommend the first approach. After all, Python is more
about readability and ease of use than clever tricks to confuse the
uninitiated.
Peter

PS: For more information, google for python and descrintro.

Jul 18 '05 #2

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

Similar topics

7
by: Ben Thomas | last post by:
Hi all, I'm having some trouble understanding the behavior of std::ostringstream. (I'm using Visual Studio .Net & STL port 4.5.3). I'll appreciate if someone can give me a little explanation of...
3
by: Mr. Clean | last post by:
Very new to XML style sheets and need some help getting this XML: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE CDSLogger SYSTEM "CDSLogger.dtd"> <CDSLogger> <ErrorObject> <BatchName>This...
15
by: drdoubt | last post by:
using namespace std In my C++ program, even after applying , I need to use the std namespace with the scope resolution operator, like, std::cout, std::vector. This I found a little bit...
1
by: bin_P19 P | last post by:
the code i have got is as follows and now im stuck <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Shopping...
106
by: xtra | last post by:
Hi Folk I have about 1000 procedures in my project. Many, many of them are along the lines of function myfuntion () as boolean on error goto er '- Dim Dbs as dao.database Dim Rst as...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
3
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display an array of objects using a GUI. My instructions are that the CD class and it's sublcass don't need to change I just need to modify class CDInventory to include the GUI. I'm not...
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: 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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.